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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. DllImport call unmanaged code

DllImport call unmanaged code

Scheduled Pinned Locked Moved C#
helpcsharpc++question
5 Posts 3 Posters 0 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.
  • K Offline
    K Offline
    Killer3
    wrote on last edited by
    #1

    INPUT : VC++ dll (abcd.dll) has funtion named DecryptString. Requirement : i need to create a dotnet project which uses this function in unmanaged dll. Steps taken public class CryptoAPI { [DllImport("abcd.dll", EntryPoint="#392")] public static extern string DecryptString(string key,string text); } When i access the function in DOTNET string EncryptedText = CryptoAPI.DecryptString(Key,PlainText); It works fine couple of times.Some times it throws up an error "object reference not set to instance of an object". Can u please help me on this? Can u tell me if i call the method - 5 times is it loaded 5 times during the runtime? or only once when i do it for the first time. for thendral

    A S 2 Replies Last reply
    0
    • K Killer3

      INPUT : VC++ dll (abcd.dll) has funtion named DecryptString. Requirement : i need to create a dotnet project which uses this function in unmanaged dll. Steps taken public class CryptoAPI { [DllImport("abcd.dll", EntryPoint="#392")] public static extern string DecryptString(string key,string text); } When i access the function in DOTNET string EncryptedText = CryptoAPI.DecryptString(Key,PlainText); It works fine couple of times.Some times it throws up an error "object reference not set to instance of an object". Can u please help me on this? Can u tell me if i call the method - 5 times is it loaded 5 times during the runtime? or only once when i do it for the first time. for thendral

      A Offline
      A Offline
      alexey N
      wrote on last edited by
      #2

      Maybe you pass to your function null or empty string ? Try to check this before call. I use this function: string.IsNullOrEmpty(string)

      K 1 Reply Last reply
      0
      • A alexey N

        Maybe you pass to your function null or empty string ? Try to check this before call. I use this function: string.IsNullOrEmpty(string)

        K Offline
        K Offline
        Killer3
        wrote on last edited by
        #3

        I have validated the function before. The gets passed the unmanaged code as well the unmanaged code returns a value but the result set is not recieved back in the DOTNET string. getting me? string EncryptedText = CryptoAPI.EncryptCString (Key,PlainText); VC++ implementation() { char * EncryptCString (Key,PlainText); { AfxMessageBox(Key); AfxMessageBox(PlainText); ///Code to do manipulatione AfxMessageBox(encryptedvalue); return encryptedvalue } } the above code snippet ensure that the values of the parameter are passed correctly to the unmanaged code and it process and return the value correctly so the interop is the problem i suppose? ie) getting it from vc++ to DOTNET ? for thendral

        A 1 Reply Last reply
        0
        • K Killer3

          I have validated the function before. The gets passed the unmanaged code as well the unmanaged code returns a value but the result set is not recieved back in the DOTNET string. getting me? string EncryptedText = CryptoAPI.EncryptCString (Key,PlainText); VC++ implementation() { char * EncryptCString (Key,PlainText); { AfxMessageBox(Key); AfxMessageBox(PlainText); ///Code to do manipulatione AfxMessageBox(encryptedvalue); return encryptedvalue } } the above code snippet ensure that the values of the parameter are passed correctly to the unmanaged code and it process and return the value correctly so the interop is the problem i suppose? ie) getting it from vc++ to DOTNET ? for thendral

          A Offline
          A Offline
          alexey N
          wrote on last edited by
          #4

          Try to set charset in DllImport attribute: [DllImport("abcd.dll", CharSet=CharSet.Ansi, EntryPoint="#392")] public static extern string DecryptString(string key,string text); And try unmanaged type conversions: [DllImport("abcd.dll", EntryPoint="#392")] public static extern [MarshalAs(UnmanagedType.LPStr)] string DecryptString(string key,string text); I don't know exactly wich type is char *. I hope you'll find it changing UnmanagedType.LPStr to other string types from UnmanagedType enum.

          1 Reply Last reply
          0
          • K Killer3

            INPUT : VC++ dll (abcd.dll) has funtion named DecryptString. Requirement : i need to create a dotnet project which uses this function in unmanaged dll. Steps taken public class CryptoAPI { [DllImport("abcd.dll", EntryPoint="#392")] public static extern string DecryptString(string key,string text); } When i access the function in DOTNET string EncryptedText = CryptoAPI.DecryptString(Key,PlainText); It works fine couple of times.Some times it throws up an error "object reference not set to instance of an object". Can u please help me on this? Can u tell me if i call the method - 5 times is it loaded 5 times during the runtime? or only once when i do it for the first time. for thendral

            S Offline
            S Offline
            shezh
            wrote on last edited by
            #5

            I had a similar function that returned a string from a C++ dll. Try declaring a global char array in your dll and then copy the string into the array, as below: // global char array char szReturn[MAX_PATH]; BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } __declspec(dllexport) LPCTSTR Test() { lstrcpy(szReturn,"success!"); return szReturn; } Hope that helps ;)

            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