Calling a C++ DLL from C#
-
I have the next DLL: ".h" #ifdef BABMD5_EXPORTS #define BABMD5_API __declspec(dllexport) #else #define BABMD5_API __declspec(dllimport) #endif // This class is exported from the BabMD5.dll class BABMD5_API CBabMD5 { public: CBabMD5(void); }; BABMD5_API long WINAPI EncodeText(LPCTSTR p_sPlainText, LPTSTR p_sEncodedText, long nBufferLen); ".cpp" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } BABMD5_API long WINAPI EncodeText(LPCTSTR p_sPlainText,LPTSTR p_sEncodedText, long nBufferLen) { CMD5 m_md5; // Here get the user name always in uppercase, // and if we've got a NULL pointer then we fill it with an empty string // We've done this way because of Visual Basic null strings if(p_sPlainText!=NULL) m_md5.setPlainText( _strupr((LPTSTR)p_sPlainText) ); else m_md5.setPlainText(""); if (nBufferLen>32) // and return the encoded string also un uppercase strcpy(p_sEncodedText,_strupr((LPTSTR)m_md5.getMD5Digest())); return 33; } // This is the constructor of a class that has been exported. // see BabMD5.h for the class definition CBabMD5::CBabMD5() { return; } I need call the function "EncodeText" from C# to do it I typed the next code: [DllImport("BabMD5.dll")] static extern long EncodeText( string p_password, [MarshalAs(UnmanagedType.LPTStr)]string p_encText, long p_size); ... try { string sText = ""; EncodeText("testestest",sText,33); } catch(DllNotFoundException exc) { Console.WriteLine(exc.ToString()); } catch(EntryPointNotFoundException exc) { Console.WriteLine(exc.ToString()); } But I doesn't return nothing. Anybody know what's happen? Thank you.
-
I have the next DLL: ".h" #ifdef BABMD5_EXPORTS #define BABMD5_API __declspec(dllexport) #else #define BABMD5_API __declspec(dllimport) #endif // This class is exported from the BabMD5.dll class BABMD5_API CBabMD5 { public: CBabMD5(void); }; BABMD5_API long WINAPI EncodeText(LPCTSTR p_sPlainText, LPTSTR p_sEncodedText, long nBufferLen); ".cpp" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } BABMD5_API long WINAPI EncodeText(LPCTSTR p_sPlainText,LPTSTR p_sEncodedText, long nBufferLen) { CMD5 m_md5; // Here get the user name always in uppercase, // and if we've got a NULL pointer then we fill it with an empty string // We've done this way because of Visual Basic null strings if(p_sPlainText!=NULL) m_md5.setPlainText( _strupr((LPTSTR)p_sPlainText) ); else m_md5.setPlainText(""); if (nBufferLen>32) // and return the encoded string also un uppercase strcpy(p_sEncodedText,_strupr((LPTSTR)m_md5.getMD5Digest())); return 33; } // This is the constructor of a class that has been exported. // see BabMD5.h for the class definition CBabMD5::CBabMD5() { return; } I need call the function "EncodeText" from C# to do it I typed the next code: [DllImport("BabMD5.dll")] static extern long EncodeText( string p_password, [MarshalAs(UnmanagedType.LPTStr)]string p_encText, long p_size); ... try { string sText = ""; EncodeText("testestest",sText,33); } catch(DllNotFoundException exc) { Console.WriteLine(exc.ToString()); } catch(EntryPointNotFoundException exc) { Console.WriteLine(exc.ToString()); } But I doesn't return nothing. Anybody know what's happen? Thank you.
Please read the article: http://www.codeproject.com/csharp/unmanage.asp your problem will be done. The Internet Give a Chance to Learn. I Do!
-
Please read the article: http://www.codeproject.com/csharp/unmanage.asp your problem will be done. The Internet Give a Chance to Learn. I Do!
The first thing that I've made was read the article, It's a good article where I found how to avoid the problem of load the dll and the method but my actual problem is that I can load and detect the method but this don't return nothing. I don't know what it's happening.
-
The first thing that I've made was read the article, It's a good article where I found how to avoid the problem of load the dll and the method but my actual problem is that I can load and detect the method but this don't return nothing. I don't know what it's happening.
If you want to return 'sText', you must ref the var. Try this: [DllImport("BabMD5.dll")] static extern long EncodeText( string p_password, ref string p_encText, long p_size); EncodeText("testestest",ref sText,33); The Internet Give a Chance to Learn. I Do!