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. -
UpdateStatus(DWORD type, DWORD action, LPTSTR buffer, DWORD bufferSize);
-
-
DLL Declare
-
private static extern int UpdateStatus(int type, int action, StringBuilder buffer, int bufferSize);
-
-
StringBuilder buffer = new StringBuilder(256);
-
// error happens here.
-
UpdateStatus(1, 1, buffer, buffer.Capacity);
-
Note that I am careful inside the DLL not to write passed the end of size passed. -
StringBuilder buffer = new StringBuilder(256);
-
// this obviously always works
-
UpdateStatus(1, 1, null, 0);
-
Any suggestions is greatly appreciated.
Thanks.
0 1886 Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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
|
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
|
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
{
| |
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....
|
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...
|
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.
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |