473,625 Members | 3,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About calling into VC++ DLL

1 New Member
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 1517

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

Similar topics

13
4098
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
2799
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
2896
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 written in C#. When the app calls my unmanaged functions, they work fine. But as soon as my unmanaged functions call managed functions (in the same source file!), the app reports an "unknown exception" error.
17
2190
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. Recently I started calling an unmanaged DLL from a .NET app. It worked fine. Now I have the situation where I am trying to call a function in one DLL from another DLL. Both are unmanaged and are built with VC++7.1. In the calling DLL I set the property...
1
1597
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 set, normalizing it to 0x00000001. If it wants to normalize the value then it should only operate on the al register since that's all that the native bool method uses to hold the return value. Is this a known VC 7.1 bug? Is there a hotfix...
16
517
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 and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
16
3774
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 compiled a DLL of the Python source code using the pythoncode VC++ project in the PCbuild folder of the source download and this works 100% without any warnings etc. I've done this in Debug and Release mode without any problems.
6
11568
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 another convention unless they have an ellipsis argument.) I can force GCC and other compilers to use such a convention by declaring all methods with an ellipsis, but I'd rather not clutter my method definitions with these.
1
2184
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 from the VC++ program no longer works I am getting an error on the #import saying "Cannot open type library file: <name of file>: Error loading type library / DLL
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1500
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.