my problem is related to marshalling of C data type 'BSTR from the .NET code
steps:
1. I have a c# program which calls one function from a VC++ dll file as
follows:
Expand|Select|Wrap|Line Numbers
- [DllImport(@"c:\Documents and Settings\skudale\My Documents\Visual Studio 2005\Projects\TestString\debug\CppExpLib.dll")]
- //internal static extern int addnum(int a, int b);
- internal static extern int testBSTR([In] InputClass input);
- private static InputClass input;
- static void Main(string[] args)
- {
- //int ans = addnum(5, 6);
- input = new InputClass();
- int iRes = testBSTR(input);
- Console.WriteLine("success");
- }
Expand|Select|Wrap|Line Numbers
- public class InputClass
- {
- [MarshalAs(UnmanagedType.LPStr)]
- private string fuserid;
- internal InputClass()
- {
- fuserid = "";
- }
- }
testBSTR which is defined in my VC++ dll.
The function defination in VC++ dll is as follows:
Expand|Select|Wrap|Line Numbers
- extern "C" int __declspec(dllexport) __stdcall testBSTR(t_sys_lgn_in *in)
- {
- char* source = "sujeet";
- psz2BStr(&in->fuserid, source);
- return 0;
- }
as follows:
Expand|Select|Wrap|Line Numbers
- #include <comdef.h>
- #include <iostream>
- typedef struct tag_sys_lgn_in
- {
- BSTR fuserid;
- } t_sys_lgn_in;
Expand|Select|Wrap|Line Numbers
- void psz2BStr(BSTR* to, char* from)
- {
- BSTR temp = *to;
- BSTR temp = SysAllocString(*to);
- *to = SysAllocStringByteLen(from, strlen(from)+1);
- if(!*to)
- {
- *to = temp;
- }
- else
- {
- SysFreeString( temp );
- }
- }
7. Now, if I turn on the "Enable unmanaged code debugging" option in
my .NET project and step into the C++ function into the DLL, I got the
following error:
"Invalid Address specified to RtlSizeHeap" at the SysFreeString( temp );
statement.
please help me on this error...
some observations:
1. If I turn of the "Enable unmanaged code debugging" option of my .NET project, program runs normally without any error.
2. The same client implementation in VB 6.0 for the same VC++ DLL
doesn't give any error.