Marshalling unmanaged DLL
-
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);
-
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);
Try marshalling your char[] as StringBuilder objects instead.
Deja View - the feeling that you've seen this post before.
-
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);
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
-
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
-
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.
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.
-
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.
-
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.
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
-
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.
-
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.
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