.net C # to call C ++ dll consists of the following steps
1, loaded dll
[DllImport("CallOtherLanguage.dll", EntryPoint = "create_prg", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] private static extern int create_prg(byte[] first_num, byte[] second_num);
2, call the dll
public void CallOther() { string first = "this is a "; byte[] byteFirst = Encoding.UTF8.GetBytes(first); string second = "other"; byte[] byteSecond = Encoding.UTF8.GetBytes(second); int result = create_prg(byteFirst, byteSecond); Console.WriteLine(result); }
Problems encountered
1, calls dll Times: Unable to load * .dll, can not find the specified module
This is mainly for two reasons
a, because there is a corresponding dll dll dependent on the other, [depends] with this program open to see what dll dll not rely on the current dll into the same directory, download the specified dll to the current directory can
b, path-dependent dll dll not put where the environment variable, the dependent variable dll into the environment will solve this error
2, the console program and winform program calls dll can return the correct content, is webform or webservice calls dll error will be: Untreated System.StackOverflowException
The cause of this is the console program and learned winform in a single thread, and webform or call webservice is multi-threaded, it may be a problem in that regard, here are two temporary solution
1, write a console program calls dll, then weborm call this console program and getting results back
2, the webform request queue or written to the database, and then writing a service to automatically poll the queue for processing database or
2, called when normal vs debugging, failure to IIS is released after the call, you can not find the specified module: Unable to load * .dll, can not find the specified module
This problem is mainly due to the path environment variable called dll where the dll depending on configuration is a user environment variable rather than the system environment change management, configuration environment variable to the system environment variables to solve problems
The reason is that IIS allows the user to tune to get any user environment variables can only be retrieved into the system environment variables