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. Unmanaged Dlls ?

Unmanaged Dlls ?

Scheduled Pinned Locked Moved C#
csharpc++tutorialquestion
12 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.
  • V Offline
    V Offline
    veselin_iordanov
    wrote on last edited by
    #1

    ok i have this case and try almost everething and dont work .. C++ Dll: typedef BYTE *CCSTR; CCSTR __stdcall Example(CCSTR data) { //do somthing } C#: [DllImport("test.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] static public extern string Example([MarshalAs(UnmanagedType.LPStr)] string Data) ; if someone have idea what can be wrong that will be very cool 10x in advance PS: Sry about my bad english

    L H 2 Replies Last reply
    0
    • V veselin_iordanov

      ok i have this case and try almost everething and dont work .. C++ Dll: typedef BYTE *CCSTR; CCSTR __stdcall Example(CCSTR data) { //do somthing } C#: [DllImport("test.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] static public extern string Example([MarshalAs(UnmanagedType.LPStr)] string Data) ; if someone have idea what can be wrong that will be very cool 10x in advance PS: Sry about my bad english

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      And the error you are receiving? leppie::AllocCPArticle("Zee blog");
      Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

      V 1 Reply Last reply
      0
      • L leppie

        And the error you are receiving? leppie::AllocCPArticle("Zee blog");
        Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

        V Offline
        V Offline
        veselin_iordanov
        wrote on last edited by
        #3

        no error just dont retutn anything

        1 Reply Last reply
        0
        • V veselin_iordanov

          ok i have this case and try almost everething and dont work .. C++ Dll: typedef BYTE *CCSTR; CCSTR __stdcall Example(CCSTR data) { //do somthing } C#: [DllImport("test.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] static public extern string Example([MarshalAs(UnmanagedType.LPStr)] string Data) ; if someone have idea what can be wrong that will be very cool 10x in advance PS: Sry about my bad english

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You have CharSet set to CharSet.Auto, but your typedef clearly shows that only an ANSI string (single byte) would be supported. Set CharSet to CharSet.Ansi.

          Microsoft MVP, Visual C# My Articles

          V 1 Reply Last reply
          0
          • H Heath Stewart

            You have CharSet set to CharSet.Auto, but your typedef clearly shows that only an ANSI string (single byte) would be supported. Set CharSet to CharSet.Ansi.

            Microsoft MVP, Visual C# My Articles

            V Offline
            V Offline
            veselin_iordanov
            wrote on last edited by
            #5

            i try it result is the same :(

            H 1 Reply Last reply
            0
            • V veselin_iordanov

              i try it result is the same :(

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Why don't you post some code. This can work and I've done it successfully in the past. You either pin the addresses yourself or use one of the methods from the System.Runtime.InteropServices.Marshal class, though you're limited in what you can do with them. All ths has to be done from within a safe context, unless you use the methods from the Marshal class and use IntPtr for the pointer fields in your struct. EDIT: Wrong reply - mistook for a different P/Invoke thread today.

              Microsoft MVP, Visual C# My Articles

              V 1 Reply Last reply
              0
              • H Heath Stewart

                Why don't you post some code. This can work and I've done it successfully in the past. You either pin the addresses yourself or use one of the methods from the System.Runtime.InteropServices.Marshal class, though you're limited in what you can do with them. All ths has to be done from within a safe context, unless you use the methods from the Marshal class and use IntPtr for the pointer fields in your struct. EDIT: Wrong reply - mistook for a different P/Invoke thread today.

                Microsoft MVP, Visual C# My Articles

                V Offline
                V Offline
                veselin_iordanov
                wrote on last edited by
                #7

                typedef BYTE *BBSTR; BBSTR AllocString(UINT len) { return (BBSTR) SysAllocStringByteLen(NULL, len); } void ReAllocString(BBSTR *pbbstr, UINT len) { BBSTR tstr; tstr = AllocString(len); if(len > StringLen(*pbbstr)) len = StringLen(*pbbstr); if(len > 0) memcpy(tstr, *pbbstr, len); FreeString(*pbbstr); *pbbstr = tstr; } BBSTR __stdcall decode(BBSTR data) { UINT i, j; BBSTR outstr; outstr = AllocString(StringLen(data) - 1); i = 0; j = 0; while(i + 1 < StringLen(data)) { //first to 2nd last char if(data[i] == 0x07) { i++; outstr[j] = data[i] ^ 0x0F; } else { outstr[j] = data[i]; } i++; j++; } ReAllocString(&outstr, j); return outstr; } in VB this is defined like Public Declare Function decode Lib "endecode.dll" (ByVal Data As String) As String in C# [DllImport("endecode.dll",CharSet=CharSet.ASCII, CallingConvention=CallingConvention.StdCall)] static public extern string decode ([MarshalAs(UnmanagedType.LPStr)] string Data) ; but dont work

                H 1 Reply Last reply
                0
                • V veselin_iordanov

                  typedef BYTE *BBSTR; BBSTR AllocString(UINT len) { return (BBSTR) SysAllocStringByteLen(NULL, len); } void ReAllocString(BBSTR *pbbstr, UINT len) { BBSTR tstr; tstr = AllocString(len); if(len > StringLen(*pbbstr)) len = StringLen(*pbbstr); if(len > 0) memcpy(tstr, *pbbstr, len); FreeString(*pbbstr); *pbbstr = tstr; } BBSTR __stdcall decode(BBSTR data) { UINT i, j; BBSTR outstr; outstr = AllocString(StringLen(data) - 1); i = 0; j = 0; while(i + 1 < StringLen(data)) { //first to 2nd last char if(data[i] == 0x07) { i++; outstr[j] = data[i] ^ 0x0F; } else { outstr[j] = data[i]; } i++; j++; } ReAllocString(&outstr, j); return outstr; } in VB this is defined like Public Declare Function decode Lib "endecode.dll" (ByVal Data As String) As String in C# [DllImport("endecode.dll",CharSet=CharSet.ASCII, CallingConvention=CallingConvention.StdCall)] static public extern string decode ([MarshalAs(UnmanagedType.LPStr)] string Data) ; but dont work

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  Next time, please use the <pre> tags around your code. If it won't compile, that's because ASCII is not an enumeration member of the CharSet enum. The compiler error would tell you this. Also, the endecode.dll library must be in your PATH environment variable somewhere (like %WINDIR%\ and %WINDIR%\System32, etc.), or in the application directory (from where the application is run, or whatever is set to the working directory). P/Invoking code from an unmanaged DLL requires that the DLL can still be found. In this case, usually, you'll get a FileNotFoundException or some other runtime exception. Since your VB code works, there's really no reason to post your C/C++ code.

                  Microsoft MVP, Visual C# My Articles

                  V 1 Reply Last reply
                  0
                  • H Heath Stewart

                    Next time, please use the <pre> tags around your code. If it won't compile, that's because ASCII is not an enumeration member of the CharSet enum. The compiler error would tell you this. Also, the endecode.dll library must be in your PATH environment variable somewhere (like %WINDIR%\ and %WINDIR%\System32, etc.), or in the application directory (from where the application is run, or whatever is set to the working directory). P/Invoking code from an unmanaged DLL requires that the DLL can still be found. In this case, usually, you'll get a FileNotFoundException or some other runtime exception. Since your VB code works, there's really no reason to post your C/C++ code.

                    Microsoft MVP, Visual C# My Articles

                    V Offline
                    V Offline
                    veselin_iordanov
                    wrote on last edited by
                    #9

                    dll is in the same directory but C# code dont work in this way

                    H 1 Reply Last reply
                    0
                    • V veselin_iordanov

                      dll is in the same directory but C# code dont work in this way

                      H Offline
                      H Offline
                      Heath Stewart
                      wrote on last edited by
                      #10

                      Um, yes it does. The CLR looks for native libraries in the same way that all other applications (unless hard-coded with a path) do. The CLR looks for managed assemblies in a different way, but the application path is still checked for assemblies. Trust me. I've been doing this since 1.0 beta days and, unlike most people these days, I do actually read everything I can about what I do. You still have never mentioned what is happening all this time. Is an exception being thrown? Is it even compiling? Are you just not getting a return value back?

                      Microsoft MVP, Visual C# My Articles

                      V 1 Reply Last reply
                      0
                      • H Heath Stewart

                        Um, yes it does. The CLR looks for native libraries in the same way that all other applications (unless hard-coded with a path) do. The CLR looks for managed assemblies in a different way, but the application path is still checked for assemblies. Trust me. I've been doing this since 1.0 beta days and, unlike most people these days, I do actually read everything I can about what I do. You still have never mentioned what is happening all this time. Is an exception being thrown? Is it even compiling? Are you just not getting a return value back?

                        Microsoft MVP, Visual C# My Articles

                        V Offline
                        V Offline
                        veselin_iordanov
                        wrote on last edited by
                        #11

                        No exeptions Just not getting a return value back

                        H 1 Reply Last reply
                        0
                        • V veselin_iordanov

                          No exeptions Just not getting a return value back

                          H Offline
                          H Offline
                          Heath Stewart
                          wrote on last edited by
                          #12

                          I just noticed, the strings are BSTRs. In this case, they are unicode and must be handled as such. The following should work:

                          [DllImport("endecode.dll", CharSet=CharSet.Unicode /* Optional in this case */,
                          CallingConvention=CallingConvention.StdCall)]
                          [return: MarshalAs(UnmanagedType.BStr)]
                          static extern string decode([MarshalAs(UnmanagedType.BStr)]string data);

                          BSTR is #defined as wchar_t, which is a wide character. This will never by ANSI on any platform. Before you posted something that implied the string was ANSI. Also, make sure your C function declaration uses the C style declaration, otherwise it will be decorated. Since VB can all it, it's probably already correct. You can make sure by using the Dependency Walker (depends.exe), which is part of the Platform SDK and - if you choose the default options when installing VS.NET - should be in the <VS.NET Install Dir>\Vc7\PlatformSDK\bin directory. Open your unmanaged library and you should see the exported function. There should be no decoration (i.e., ordinals, etc.) on the function. If there is, you can forward-declare it like so:

                          #ifdef __cplusplus
                          extern "C" {
                          #endif
                          BSTR __stdcall decode(BSTR data);
                          #ifdef __cplusplus
                          }
                          #endif

                          You can use a .DEP file, too, but that's "out-dated".

                          Microsoft MVP, Visual C# My Articles

                          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