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. Marshalling unmanaged DLL

Marshalling unmanaged DLL

Scheduled Pinned Locked Moved C#
performancehelp
9 Posts 5 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.
  • D Offline
    D Offline
    Dio22
    wrote on last edited by
    #1

    I'm trying to use P/Invoke to marshal some legacy code. I either get an "Entry Point Not Found" error, or a "Memory access violation" I've run dumpbin on this dll, and i've verified that the entry point exists. I'm lost. Here's the source for the function I'm exporting..

    extern "C" BOOL FindUser2(char *cpUserFile, 
                  char *cpPassword, 
                  char *cpUserName, 
                  char *cpUserPriveledge, 
                  char *cpUserID, 
                  char *cpComments, 
                  BOOL *bUserActive,
                  char *cpErrorReason)
    {...}
    

    Here's my pathetic attempt to marshal it.

    [DllImport("WN_USER_SECURITY.dll",EntryPoint="FindUser2") ]
            public static extern bool FindUser2(
                [MarshalAs(UnmanagedType.LPArray)]char[] strFileName,
                [MarshalAs(UnmanagedType.LPArray)]char[] strUserPassword,
                [MarshalAs(UnmanagedType.LPArray)]char[] strUserName,
                [MarshalAs(UnmanagedType.LPArray)]char[] strUserPriveledge,
                [MarshalAs(UnmanagedType.LPArray)]char[] strUserID,
                [MarshalAs(UnmanagedType.LPArray)]char[] strUserComments,
                [MarshalAs(UnmanagedType.Bool)] bool bUserActive,
                [MarshalAs(UnmanagedType.LPArray)]char[] strErrorReason);
    
    P L 2 Replies Last reply
    0
    • D Dio22

      I'm trying to use P/Invoke to marshal some legacy code. I either get an "Entry Point Not Found" error, or a "Memory access violation" I've run dumpbin on this dll, and i've verified that the entry point exists. I'm lost. Here's the source for the function I'm exporting..

      extern "C" BOOL FindUser2(char *cpUserFile, 
                    char *cpPassword, 
                    char *cpUserName, 
                    char *cpUserPriveledge, 
                    char *cpUserID, 
                    char *cpComments, 
                    BOOL *bUserActive,
                    char *cpErrorReason)
      {...}
      

      Here's my pathetic attempt to marshal it.

      [DllImport("WN_USER_SECURITY.dll",EntryPoint="FindUser2") ]
              public static extern bool FindUser2(
                  [MarshalAs(UnmanagedType.LPArray)]char[] strFileName,
                  [MarshalAs(UnmanagedType.LPArray)]char[] strUserPassword,
                  [MarshalAs(UnmanagedType.LPArray)]char[] strUserName,
                  [MarshalAs(UnmanagedType.LPArray)]char[] strUserPriveledge,
                  [MarshalAs(UnmanagedType.LPArray)]char[] strUserID,
                  [MarshalAs(UnmanagedType.LPArray)]char[] strUserComments,
                  [MarshalAs(UnmanagedType.Bool)] bool bUserActive,
                  [MarshalAs(UnmanagedType.LPArray)]char[] strErrorReason);
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Try marshalling your char[] as StringBuilder objects instead.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      1 Reply Last reply
      0
      • D Dio22

        I'm trying to use P/Invoke to marshal some legacy code. I either get an "Entry Point Not Found" error, or a "Memory access violation" I've run dumpbin on this dll, and i've verified that the entry point exists. I'm lost. Here's the source for the function I'm exporting..

        extern "C" BOOL FindUser2(char *cpUserFile, 
                      char *cpPassword, 
                      char *cpUserName, 
                      char *cpUserPriveledge, 
                      char *cpUserID, 
                      char *cpComments, 
                      BOOL *bUserActive,
                      char *cpErrorReason)
        {...}
        

        Here's my pathetic attempt to marshal it.

        [DllImport("WN_USER_SECURITY.dll",EntryPoint="FindUser2") ]
                public static extern bool FindUser2(
                    [MarshalAs(UnmanagedType.LPArray)]char[] strFileName,
                    [MarshalAs(UnmanagedType.LPArray)]char[] strUserPassword,
                    [MarshalAs(UnmanagedType.LPArray)]char[] strUserName,
                    [MarshalAs(UnmanagedType.LPArray)]char[] strUserPriveledge,
                    [MarshalAs(UnmanagedType.LPArray)]char[] strUserID,
                    [MarshalAs(UnmanagedType.LPArray)]char[] strUserComments,
                    [MarshalAs(UnmanagedType.Bool)] bool bUserActive,
                    [MarshalAs(UnmanagedType.LPArray)]char[] strErrorReason);
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Hi first, as pete pointed out already it's a good idea to pass the char* with a string builder. second, the BOOL *bUserActive looks to me like a ref value. so you might have to change your call as follows:

        [DllImport("WN_USER_SECURITY.dll",EntryPoint="FindUser2") ]
        public static extern bool FindUser2(
        StringBuilder strFileName,
        StringBuilder strUserPassword,
        StringBuilder strUserName,
        StringBuilder strUserPriveledge,
        StringBuilder strUserID,
        StringBuilder strUserComments,
        [In,Out /*this is optional*/]ref bool bUserActive,
        StringBuilder strErrorReason);

        it might work without the marshalAs Attributes because on "normal" datatypes, the clr automatically marshals the data correctly. hope this helps m@u

        D 1 Reply Last reply
        0
        • L Lost User

          Hi first, as pete pointed out already it's a good idea to pass the char* with a string builder. second, the BOOL *bUserActive looks to me like a ref value. so you might have to change your call as follows:

          [DllImport("WN_USER_SECURITY.dll",EntryPoint="FindUser2") ]
          public static extern bool FindUser2(
          StringBuilder strFileName,
          StringBuilder strUserPassword,
          StringBuilder strUserName,
          StringBuilder strUserPriveledge,
          StringBuilder strUserID,
          StringBuilder strUserComments,
          [In,Out /*this is optional*/]ref bool bUserActive,
          StringBuilder strErrorReason);

          it might work without the marshalAs Attributes because on "normal" datatypes, the clr automatically marshals the data correctly. hope this helps m@u

          D Offline
          D Offline
          Dio22
          wrote on last edited by
          #4

          Well, I still can't get past the "Entry point not found" exception. I've examined the DLL using dumpbin and Dependency Walker, and they both show the entry point "FindUser2" as existing at ordinal 1. For some reason the C# app just wont see it.

          A L L 3 Replies Last reply
          0
          • D Dio22

            Well, I still can't get past the "Entry point not found" exception. I've examined the DLL using dumpbin and Dependency Walker, and they both show the entry point "FindUser2" as existing at ordinal 1. For some reason the C# app just wont see it.

            A Offline
            A Offline
            Andre Azevedo
            wrote on last edited by
            #5

            Did you get the exception in application run or in method calling?

            ________________________________________________________________ There are 10 kind of people: those who knows binary and those who doesn't.

            D 1 Reply Last reply
            0
            • A Andre Azevedo

              Did you get the exception in application run or in method calling?

              ________________________________________________________________ There are 10 kind of people: those who knows binary and those who doesn't.

              D Offline
              D Offline
              Dio22
              wrote on last edited by
              #6

              The exception occurs when I try to call the method.

              A 1 Reply Last reply
              0
              • D Dio22

                Well, I still can't get past the "Entry point not found" exception. I've examined the DLL using dumpbin and Dependency Walker, and they both show the entry point "FindUser2" as existing at ordinal 1. For some reason the C# app just wont see it.

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

                Hi, here are some suggestions: 1. make sure there is only one WN_USER_SECURITY.dll file. Maybe you are looking at one file with dumpbin, but your app is looking at another (older) one, hidden in the Windows PATH somewhere. 2. Does the app find other methods in the DLL? does another app work fine with that DLL? :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


                1 Reply Last reply
                0
                • D Dio22

                  The exception occurs when I try to call the method.

                  A Offline
                  A Offline
                  Andre Azevedo
                  wrote on last edited by
                  #8

                  Dio22 wrote:

                  The exception occurs when I try to call the method

                  So the function is there. Maybe you´re checking another dll.

                  ________________________________________________________________ There are 10 kind of people: those who knows binary and those who doesn't.

                  1 Reply Last reply
                  0
                  • D Dio22

                    Well, I still can't get past the "Entry point not found" exception. I've examined the DLL using dumpbin and Dependency Walker, and they both show the entry point "FindUser2" as existing at ordinal 1. For some reason the C# app just wont see it.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    hmm.. i'm not a c++ guru, but i once wrote a little c++ demo lib that was callable from c# and i'm not sure if this is important, and if you can do anything about it but: 1) in my method-declaration i used the stdCall keyword: void __stdcall foo(unsigned int *param1, unsigned char *param2, unsigned char *param3, void *Handle) --> i think the default calling convention for pInvoked Method is stdCall so if it's not declared that way it might fail.. 2) in the exports.def of the library i had to put this in order to make it work: EXPORTS foo @1 --> if this is missing, it might explain the EntrypointNotFound - exception greets m@u

                    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