Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DLLImport problem

DLLImport problem

Scheduled Pinned Locked Moved C#
questioncsharpperformancehelpworkspace
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    teejayem
    wrote on last edited by
    #1

    I have an unmanaged dll and I'm trying to make a wrapper around it so it can be used in a multiple projects. I have most of the functions working accept 1 and i can't seem to figure it out. From the pdf the signature is as follows:

    ic_subcall (sub_name, sub_name_len, code, num_args, var_args...)

    In the documentation, the last argument is similar to params in C# and it takes type "ICSTRING".

    typedef struct icstring {
    long len;
    unsigned char * text;
    } ICSTRING;

    And here is the method signature in the header file:

    void FAR ic_subcall ic_proto((LPSTR, LPLONG, LPLONG, LPLONG, ...));

    This is how i have my struct setup in C#. Here i am using a byte[] because i found (somewhere) that it is the equivelant to an unsighed char.

    [StructLayout(LayoutKind.Sequential)]
    internal struct icstring
    {
    public long len;
    public byte[] text;
    }

    And finally, here is where i using the DLLImport

    [DllImport("uvic32.dll", SetLastError = true)]
    private static extern void ic_subcall(string sub_name, long sub_name_len, ref long code, long num_args, params icstring[] var_args);

    When i make the call it throws an exception "Attempted to read protected memory....". I can't seem to figure out what is wrong. I'm not sure if it is the params keyword or if my struct is messed up somehow. Could someone point me in the right direction?

    Don't be overcome by evil, but overcome evil with good

    L L 2 Replies Last reply
    0
    • T teejayem

      I have an unmanaged dll and I'm trying to make a wrapper around it so it can be used in a multiple projects. I have most of the functions working accept 1 and i can't seem to figure it out. From the pdf the signature is as follows:

      ic_subcall (sub_name, sub_name_len, code, num_args, var_args...)

      In the documentation, the last argument is similar to params in C# and it takes type "ICSTRING".

      typedef struct icstring {
      long len;
      unsigned char * text;
      } ICSTRING;

      And here is the method signature in the header file:

      void FAR ic_subcall ic_proto((LPSTR, LPLONG, LPLONG, LPLONG, ...));

      This is how i have my struct setup in C#. Here i am using a byte[] because i found (somewhere) that it is the equivelant to an unsighed char.

      [StructLayout(LayoutKind.Sequential)]
      internal struct icstring
      {
      public long len;
      public byte[] text;
      }

      And finally, here is where i using the DLLImport

      [DllImport("uvic32.dll", SetLastError = true)]
      private static extern void ic_subcall(string sub_name, long sub_name_len, ref long code, long num_args, params icstring[] var_args);

      When i make the call it throws an exception "Attempted to read protected memory....". I can't seem to figure out what is wrong. I'm not sure if it is the params keyword or if my struct is messed up somehow. Could someone point me in the right direction?

      Don't be overcome by evil, but overcome evil with good

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, AFAIK varargs cannot be passed from managed to unmanaged worlds, that is without the managed world investigating all the arguments and Marshaling them explicitly one by one. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      T 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, AFAIK varargs cannot be passed from managed to unmanaged worlds, that is without the managed world investigating all the arguments and Marshaling them explicitly one by one. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        T Offline
        T Offline
        teejayem
        wrote on last edited by
        #3

        Hey Luc! Thats not good news! :sigh: I did a search on DLLImport & varargs and i was able to find someonthing HERE[^]. Right above the examples it explains the enum types. I found

        Cdecl -- The caller cleans the stack. This enables calling functions with varargs,
        which makes it appropriate to use for methods that accept a variable number of parameters, such as Printf.

        I see in the example how they are using it in the printf. Do you think if i was to change my signature to

        private static extern void ic_subcall(string sub_name, long sub_name_len, ref long code, long num_args, icstring var_args1, icstring var_args2, ..., ...);

        and used the calling convention documented that i would work?

        Don't be overcome by evil, but overcome evil with good

        L 1 Reply Last reply
        0
        • T teejayem

          I have an unmanaged dll and I'm trying to make a wrapper around it so it can be used in a multiple projects. I have most of the functions working accept 1 and i can't seem to figure it out. From the pdf the signature is as follows:

          ic_subcall (sub_name, sub_name_len, code, num_args, var_args...)

          In the documentation, the last argument is similar to params in C# and it takes type "ICSTRING".

          typedef struct icstring {
          long len;
          unsigned char * text;
          } ICSTRING;

          And here is the method signature in the header file:

          void FAR ic_subcall ic_proto((LPSTR, LPLONG, LPLONG, LPLONG, ...));

          This is how i have my struct setup in C#. Here i am using a byte[] because i found (somewhere) that it is the equivelant to an unsighed char.

          [StructLayout(LayoutKind.Sequential)]
          internal struct icstring
          {
          public long len;
          public byte[] text;
          }

          And finally, here is where i using the DLLImport

          [DllImport("uvic32.dll", SetLastError = true)]
          private static extern void ic_subcall(string sub_name, long sub_name_len, ref long code, long num_args, params icstring[] var_args);

          When i make the call it throws an exception "Attempted to read protected memory....". I can't seem to figure out what is wrong. I'm not sure if it is the params keyword or if my struct is messed up somehow. Could someone point me in the right direction?

          Don't be overcome by evil, but overcome evil with good

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          teejayem wrote:

          I have an unmanaged dll and I'm trying to make a wrapper around it so it can be used in a multiple projects.

          In my experience creating a managed wrapper for native code is far simpler using C++/CLI than trying to do it in C#.

          led mike

          T 1 Reply Last reply
          0
          • T teejayem

            Hey Luc! Thats not good news! :sigh: I did a search on DLLImport & varargs and i was able to find someonthing HERE[^]. Right above the examples it explains the enum types. I found

            Cdecl -- The caller cleans the stack. This enables calling functions with varargs,
            which makes it appropriate to use for methods that accept a variable number of parameters, such as Printf.

            I see in the example how they are using it in the printf. Do you think if i was to change my signature to

            private static extern void ic_subcall(string sub_name, long sub_name_len, ref long code, long num_args, icstring var_args1, icstring var_args2, ..., ...);

            and used the calling convention documented that i would work?

            Don't be overcome by evil, but overcome evil with good

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            OK, I never tried it this way. I keep my P/Invoke stuff always as simple as possible. Seems like maintaining stack will not be a problem, but I am still very worried about marshaling types. Of course, simple value types (int, double, ...) and strings will probably just work fine, but what about structures, class objects, etc? :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Voting for dummies? No thanks. X|


            T 1 Reply Last reply
            0
            • L led mike

              teejayem wrote:

              I have an unmanaged dll and I'm trying to make a wrapper around it so it can be used in a multiple projects.

              In my experience creating a managed wrapper for native code is far simpler using C++/CLI than trying to do it in C#.

              led mike

              T Offline
              T Offline
              teejayem
              wrote on last edited by
              #6

              hey led mike, Yeah, I actually already have the C++ implementation and i did create a C++/CLI wrapper around it. But when u deploy the CLI dll to the target machine the C++ redistributable package needs to be installed or the dlls need to be deployed w/ it and my boss doesn't wanna deal with that :(

              Don't be overcome by evil, but overcome evil with good

              L 1 Reply Last reply
              0
              • T teejayem

                hey led mike, Yeah, I actually already have the C++ implementation and i did create a C++/CLI wrapper around it. But when u deploy the CLI dll to the target machine the C++ redistributable package needs to be installed or the dlls need to be deployed w/ it and my boss doesn't wanna deal with that :(

                Don't be overcome by evil, but overcome evil with good

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                teejayem wrote:

                and my boss doesn't wanna deal with that

                Well your boss is wrong and probably an idiot. I feel your pain brother. :-D Also, have you verified that? I wouldn't be surprised if the VC runtime DLL's are installed as part of the .NET platform.

                led mike

                T 1 Reply Last reply
                0
                • L Luc Pattyn

                  OK, I never tried it this way. I keep my P/Invoke stuff always as simple as possible. Seems like maintaining stack will not be a problem, but I am still very worried about marshaling types. Of course, simple value types (int, double, ...) and strings will probably just work fine, but what about structures, class objects, etc? :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Voting for dummies? No thanks. X|


                  T Offline
                  T Offline
                  teejayem
                  wrote on last edited by
                  #8

                  I tried it and it still didn't work. But i think your right, i think its the struct that is messing it up. Thanks anyways for the help!

                  Don't be overcome by evil, but overcome evil with good

                  1 Reply Last reply
                  0
                  • L led mike

                    teejayem wrote:

                    and my boss doesn't wanna deal with that

                    Well your boss is wrong and probably an idiot. I feel your pain brother. :-D Also, have you verified that? I wouldn't be surprised if the VC runtime DLL's are installed as part of the .NET platform.

                    led mike

                    T Offline
                    T Offline
                    teejayem
                    wrote on last edited by
                    #9

                    We're a furniture company, and the latest technology isn't in the companies best interest :(. But, i did verify it. I actually had the problem before on a personal project, and i found that if a C++/CLI, MFC, or Win32 app is created from vs the target machine is supposed to have Microsoft Visual C++ 2008 Redistributable Package (x86)[^] (there's also one for 2005). Maybe there is some setting to fix that, but i didn't look into it too much. I do think it is pretty stupid that even a pure win32 application depends on these dlls!

                    Don't be overcome by evil, but overcome evil with good

                    modified on Wednesday, July 30, 2008 7:48 AM

                    L 1 Reply Last reply
                    0
                    • T teejayem

                      We're a furniture company, and the latest technology isn't in the companies best interest :(. But, i did verify it. I actually had the problem before on a personal project, and i found that if a C++/CLI, MFC, or Win32 app is created from vs the target machine is supposed to have Microsoft Visual C++ 2008 Redistributable Package (x86)[^] (there's also one for 2005). Maybe there is some setting to fix that, but i didn't look into it too much. I do think it is pretty stupid that even a pure win32 application depends on these dlls!

                      Don't be overcome by evil, but overcome evil with good

                      modified on Wednesday, July 30, 2008 7:48 AM

                      L Offline
                      L Offline
                      led mike
                      wrote on last edited by
                      #10

                      teejayem wrote:

                      I do think it is pretty stupid that even a pure win32 application depends on these dlls!

                      Depending on the VC++ version and the target environment they can already be there. This is what I am talking about. What version of the .NET platform are you requiring be installed. I imagine some version of the C++ runtime is installed with that. Use the version of VC++ that matches that and you might be able to avoid using the redistributable.

                      led mike

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups