473,474 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB Marshal problem

Hello,

I've declared a VB callback Sub which is called by an unmanaged DLL (code
below). The PData parameter is a pointer to a memory bloc which is allocated
and freed by the dll as necessary.

The RequestFunc should read a string pointed by PData, and replace it by
another one. The problem arises when the Sub tries to write the answer :
there is an error when calling Marshal.PtrToStringAnsi. (The RequestFunc is
not supposed to free the memory bloc pointed by PData)

What's wrong with the following code ? (note : the buffer is allocated for
sure, and is large enough)

Many thanks for comments,
Sincerely
Vincent
Sub RequestFunc(ByVal PData As IntPtr, ByVal DataSize As Int32)

Dim Request As String
Dim Answer As String

Request = Marshal.PtrToStringAnsi(PData, DataSize)
MsgBox(Request)
Answer = "This is the answer"
Marshal.StructureToPtr(Answer, PData, False)
End Sub
Nov 20 '05 #1
3 2837
Vincent,
What's wrong with the following code ? (note : the buffer is allocated for
sure, and is large enough)


The problem is that a string isn't a structure. Try this instead

Dim chars() As Char = (Answer & ChrW(0)).ToCharArray()
Marshal.Copy(chars, 0, PData, Math.Min(chars.Length, DataSize))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #2

Mattias,

Thanks a lot for your help, it almost works now. However there is still a
small problem : The dll was expecting an Ansi string, not a unicode one.

If there is a simple way to convert the data which goes into the buffer to a
Ansi string, I would be happy to know it. In any case, it's not a problem to
modify the dll so that it recognizes the unicode string.

Have a nice day,
Vincent

"Mattias Sjögren" <ma********************@mvps.org> a écrit dans le message
de news:ue**************@TK2MSFTNGP11.phx.gbl...
Vincent,
What's wrong with the following code ? (note : the buffer is allocated forsure, and is large enough)


The problem is that a string isn't a structure. Try this instead

Dim chars() As Char = (Answer & ChrW(0)).ToCharArray()
Marshal.Copy(chars, 0, PData, Math.Min(chars.Length, DataSize))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 20 '05 #3
Vincent,
Thanks a lot for your help, it almost works now. However there is still a
small problem : The dll was expecting an Ansi string, not a unicode one.


Ok, then you can do

Dim chars() As Byte = System.Text.Encoding.Default.GetBytes(Answer &
ChrW(0))
Marshal.Copy(chars, 0, PData, Math.Min(chars.Length, DataSize))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: syd | last post by:
Hello all, In my project, I have container classes holding lists of item classes. For example, a container class myLibrary might hold a list of item classes myNation and associated variables...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
6
by: Howard Kaikow | last post by:
Given: private struct PROCESSENTRY32 { public int dwSize; public int cntUsage; public int th32ProcessID; public int th32DefaultHeapID; public int th32ModuleID;
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
6
by: william.thorpe.b | last post by:
I have recently switched from VS2003 to VS2005 and at the same time from V1 to V2 of the .NET Compact Framework. The target is a Windows CE 5.0 device and an ARMV4I processor. ...
2
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This...
5
by: Anurag | last post by:
I have been chasing a problem in my code since hours and it bolis down to this import marshal marshal.dumps(str(123)) != marshal.dumps(str("123")) Can someone please tell me why? when...
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
10
by: bkustel | last post by:
I'm stuck on a problem where I want to use marshal for serialization (yes, yes, I know (c)Pickle is normally recommended here). I favor marshal for speed for the types of data I use. However it...
0
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
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
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,...
1
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...
0
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...
0
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,...
1
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...
0
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...
0
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 ...
0
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...

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.