0

I know that there are many solutions for my problem. I have tried them all; however, I still get error.

These are original functions from DLL 'KeygenLibrary.dll' :

bool dfcDecodeMachineID(char* sEncodeMachineID, int iInLen, char* sMachineID, int& iOutLen);

bool dfcCreateLicense(char* sMachineID, int iLen, char* sLicenseFilePath);

To import this DLL, I have tried:

Way 1:

unsafe public class ImportDLL
{
    [DllImport("KeygenLibrary.dll", EntryPoint = "dfcDecodeMachineID")]
    unsafe public static extern bool dfcDecodeMachineID(char* sEncodeMachineID, int iInLen, char* sMachineID, ref int iOutLen);

    [DllImport("KeygenLibrary.dll", EntryPoint = "dfcCreateLicense")]
    unsafe public static extern bool dfcCreateLicense(char* sMachineID, int iLen, char* sLicenseFilePath);
}    

Way 2:

public class ImportDLL
{
    [DllImport("KeygenLibrary.dll", EntryPoint = "dfcDecodeMachineID")]
    public static extern bool dfcDecodeMachineID([MarshalAs(UnmanagedType.LPStr)] string sEncodeMachineID, int iInLen, [MarshalAs(UnmanagedType.LPStr)] string sMachineID, ref int iOutLen);


    [DllImport("KeygenLibrary.dll", EntryPoint = "dfcCreateLicense")]
    public static extern bool dfcCreateLicense([MarshalAs(UnmanagedType.LPStr)] string sMachineID, int iLen, [MarshalAs(UnmanagedType.LPStr)] string sLicenseFilePath);
}

However, both above ways give me error:

Unable to find an entry point named 'function name' in DLL 'KeygenLibrary.dll'.

How can I fix my problem? Thanks a lot.

Just A Name
  • 19
  • 1
  • 6
  • You'll want to have an `extern "C"` to prevent name mangling, as well as `__declspec(dllexport)`. And if those `char *`s are being modified, you'll have to use a `StringBuilder`. – chris Dec 25 '13 at 07:11
  • I will add extern "c" to my C# code or C++? I cant edit C++ file. – Just A Name Dec 25 '13 at 07:13
  • SUGGESTION: run "dumpbin" on your .dll. If the names look more like this: `?Close@CHIDDevice@@QAEEXZ` (instead of `Close`), then you know the problem is C++ name mangling. – paulsm4 Dec 25 '13 at 07:16
  • C++, and that's unfortunate because now you need to know the calling convention and/or the mangled name (you can use one to figure out the other). Perhaps a symbol table. Disclaimer: I've never tried without. – chris Dec 25 '13 at 07:16

1 Answers1

1

SUGGESTION:

1) Run "dumpbin" on your .dll to confirm the problem is indeed name mangling.

2) If so, try the suggestion in this link:

Entry Point Not Found Exception

a) Use undname to get the undecorated name

b) Set EntryPointy == the mangled name

c) Set CallingConvention = CallingConvention.Cdecl

d) Use the unmangled name and signature for your C# method signature

See also this link:

http://bytes.com/topic/c-sharp/answers/428504-c-program-calling-c-dll

Laurent.... You can call the function using the mangled name as in:

To call a function using its fully decorated name "?fnWin32Test2@@YAJXZ" as

"Win32Test2" you can specify the static entry point as "?fnWin32Test2@@YAJXY":

[DllImport("Win32Test.dll", EntryPoint= "?fnWin32Test2@@YAJXZ")]
public static extern int fnWin32Test2();

And call it as:

System.Console.WriteLine(fnWin32Test2());

To look at the undecorated name use the undname tool as in:

`undname ?fnWin32Test@@3HA`

This converts the decorated name to "long fnWin32Test".

Regards, Jeff

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190