473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

About calling into VC++ DLL

1
Hello,
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
  1. [DllImport(@"c:\Documents and Settings\skudale\My Documents\Visual Studio 2005\Projects\TestString\debug\CppExpLib.dll")]
  2.         //internal static extern int addnum(int a, int b);
  3.         internal static extern int testBSTR([In] InputClass input);
  4.  
  5.         private static InputClass input;
  6.  
  7.         static void Main(string[] args)
  8.         {
  9.             //int ans = addnum(5, 6);
  10.             input = new InputClass();
  11.             int iRes = testBSTR(input);
  12.  
  13.             Console.WriteLine("success");
  14.         }
  15.  
2. The defination for the Input class is follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class InputClass
  3.         {
  4.             [MarshalAs(UnmanagedType.LPStr)]
  5.             private string fuserid;
  6.  
  7.             internal InputClass()
  8.             {
  9.                 fuserid = "";
  10.             }
  11.        }
  12.  
3. I create the object of input class which is passed to a function named
testBSTR which is defined in my VC++ dll.
The function defination in VC++ dll is as follows:

Expand|Select|Wrap|Line Numbers
  1. extern "C" int __declspec(dllexport) __stdcall testBSTR(t_sys_lgn_in *in)
  2. {
  3.     char* source = "sujeet";
  4.  
  5.     psz2BStr(&in->fuserid, source);
  6.  
  7.     return 0;
  8. }
  9.  
4. t_sys_lgn_in is structure defined in one of the header file in my VC++ dll
as follows:

Expand|Select|Wrap|Line Numbers
  1. #include <comdef.h>
  2. #include <iostream>
  3.  
  4. typedef struct tag_sys_lgn_in
  5. {
  6.     BSTR          fuserid;
  7. } t_sys_lgn_in;
  8.  
5. the defination of psz2BStr is as follows:

Expand|Select|Wrap|Line Numbers
  1. void psz2BStr(BSTR* to, char* from)
  2. {
  3.      BSTR temp = *to;
  4.      BSTR temp = SysAllocString(*to);
  5.      *to = SysAllocStringByteLen(from, strlen(from)+1);
  6.  
  7.      if(!*to)
  8.      {
  9.           *to = temp;
  10.      }
  11.      else 
  12.      {
  13.           SysFreeString( temp  );
  14.      }
  15. }
  16.  
6. When I run my .NET client normally, program returns with no error.

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.
Oct 18 '06 #1
0 1509

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: RainBow | last post by:
Hi everyone, (Very Sorry, if this is the wrong group in which I am posting this query). Code snippet: //C library typedef int (*PFunc)(int* aArg); void call_c_foo(PFunc aPtrtoFunc) {
24
by: songie D | last post by:
Can managed C++ be trusted to handle the garbage collector correctly in the right bit if I have a project with unmanaged and managed parts in it?
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
17
by: Bill Grigg | last post by:
I have been successfully calling DLL's using VC++ 6.0 and also using VC++7.1 (.NET). I only mention this because I have never felt comfortable with the process, but nonetheless it did work....
1
by: Bern McCarty | last post by:
What do you make of this? I cannot tell for sure but it almost seems as the the transition thunk to get back from the native bool method to the managed caller is looking at eax and, if any bit is...
16
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
16
by: Jaco Naude | last post by:
Hi there, This is my first post over here and I hope someone can give me some guidance. I'm trying to embed Python into a Visual C++ 2008 application and I'm getting linker problems. I've...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
1
by: Clint Jensen | last post by:
Hello, I had a working project creating in Visual Studio 2003 where a VC++ program was calling a DLL produced by VB built using VS 2003. We recently upgraded to VS 2005 and calling the VB DLL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.