Unmanaged Dlls ?
-
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
-
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.no error just dont retutn anything
-
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
You have
CharSet
set toCharSet.Auto
, but your typedef clearly shows that only an ANSI string (single byte) would be supported. SetCharSet
toCharSet.Ansi
.Microsoft MVP, Visual C# My Articles
-
You have
CharSet
set toCharSet.Auto
, but your typedef clearly shows that only an ANSI string (single byte) would be supported. SetCharSet
toCharSet.Ansi
.Microsoft MVP, Visual C# My Articles
i try it result is the same :(
-
i try it result is the same :(
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 theMarshal
class and useIntPtr
for the pointer fields in your struct. EDIT: Wrong reply - mistook for a different P/Invoke thread today.Microsoft MVP, Visual C# My Articles
-
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 theMarshal
class and useIntPtr
for the pointer fields in your struct. EDIT: Wrong reply - mistook for a different P/Invoke thread today.Microsoft MVP, Visual C# My Articles
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 likePublic 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 -
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 likePublic 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 workNext time, please use the
<pre>
tags around your code. If it won't compile, that's becauseASCII
is not an enumeration member of theCharSet
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 aFileNotFoundException
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
-
Next time, please use the
<pre>
tags around your code. If it won't compile, that's becauseASCII
is not an enumeration member of theCharSet
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 aFileNotFoundException
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
dll is in the same directory but C# code dont work in this way
-
dll is in the same directory but C# code dont work in this way
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
-
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
No exeptions Just not getting a return value back
-
No exeptions Just not getting a return value back
I just noticed, the strings are
BSTR
s. 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 aswchar_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
}
#endifYou can use a .DEP file, too, but that's "out-dated".
Microsoft MVP, Visual C# My Articles