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. Calling a C++ DLL from C#

Calling a C++ DLL from C#

Scheduled Pinned Locked Moved C#
c++csharpquestion
4 Posts 2 Posters 7 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.
  • J Offline
    J Offline
    Jose Fco Bonnin
    wrote on last edited by
    #1

    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.

    A 1 Reply Last reply
    0
    • J Jose Fco Bonnin

      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.

      A Offline
      A Offline
      aner_glx
      wrote on last edited by
      #2

      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!

      J 1 Reply Last reply
      0
      • A aner_glx

        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!

        J Offline
        J Offline
        Jose Fco Bonnin
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • J Jose Fco Bonnin

          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.

          A Offline
          A Offline
          aner_glx
          wrote on last edited by
          #4

          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!

          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