473,789 Members | 2,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# - How to pin the StringBuilder Object when passing to Unmanaged Code

1 New Member
Hello,

I need to pass a StringBuilder object to a custom unmanaged DLL. The code most of the time works great, however once in a while I get "Object reference not set to an instance of an object" which means the GC moved the address of StringBuilder object while managed code is writing to it.

I have tried GC.keepalive already. GCHandle.allc and Marshal.alloc doesn't seem to be possible with this type of object. I need the equivalent of (__pin *) in managed C++ to pin the object somehow.

Expand|Select|Wrap|Line Numbers
  1. UpdateStatus(DWORD type, DWORD action, LPTSTR buffer, DWORD bufferSize);
  2.  
  3. DLL Declare
  4. private static extern int UpdateStatus(int type, int action, StringBuilder buffer, int bufferSize);
  5.  
Expand|Select|Wrap|Line Numbers
  1. StringBuilder buffer = new StringBuilder(256);
  2. // error happens here.
  3. UpdateStatus(1, 1, buffer, buffer.Capacity); 
  4.  
Note that I am careful inside the DLL not to write passed the end of size passed.

Expand|Select|Wrap|Line Numbers
  1. StringBuilder buffer = new StringBuilder(256);
  2. // this obviously always works 
  3. UpdateStatus(1, 1, null, 0); 
  4.  
Any suggestions is greatly appreciated.
Thanks.
Feb 20 '07 #1
0 1886

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

Similar topics

1
3552
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
14
15052
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a string input parameter. Now if I define the function to take in a string array, it solves my...
0
8774
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR pchMessage; // decoded message data TCHAR chCodeID; // AIM Id of symbology TCHAR chSymLetter; // HHP Id of symbology
3
4711
by: Salvador | last post by:
Hi, I am using an old Win32 DLL that expects a LPBYTE as a parameter. The Stringbuilder appears to be working, the problem is that the component is sending me a string with NULL characters in the middle, and my StringBuilder is shrinking the message. Example StringBuilder buffer = new StringBuilder(255); Win32 sends : VB123421414\0\0\0\0HRA0021331 StringBuilder reads : VB123421414
1
1867
by: cppdev | last post by:
Hello, After reading a few articles, http://blogs.gotdotnet.com/cbrumme/PermaLink.aspx/e55664b4-6471-48b9-b360-f0fa27ab6cc0 http://blogs.gotdotnet.com/anathan/commentview.aspx/8ec0b7b2-6290-4500-a8c7-1b6c677214cb i have the following question: __gc class C {
3
8708
by: Morgan Cheng | last post by:
In P/Invoke situation, If some *out* parameter is LPWSTR, I can use string or StringBuilder. However, there is one problem about StringBuilder. By default, its Capacity is 16. If the returned string length is larger than 16. An ArgumentOutOfRangeExceptoin is raised. However, if string is used, no exception. I searched this topic. It is said that StringBuilder is preferred in this case, because the performance is better. I don't think so....
8
5899
by: Notre Poubelle | last post by:
Hello, I have some legacy unmanaged C++ code in which I've introduced some managed C++ using the new CLI feature of VS 2005. My managed C++ code invokes a number of methods on a new C#class I've written. This C# class creates additional managed objects and passes one of them back to the managed C++ code. I believe I have everything right to this point. I need to briefly pass the managed object into unmanaged C++ code before...
8
2917
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory doesn't get released (or actually it looks like that) and thus the used memory keeps growing. Is there a solution to this? Regards, Arjan.
9
3567
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The 3rd party unmanaged DLL will pass this reference into standard Win32 unmanaged static callback function in my code. Inside this unmanaged callback function I need to cast this unmnaged pointer that I have received from 3rd party back into the...
0
10410
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...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
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
9020
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
7529
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
6769
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
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.