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 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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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;
|
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...
|
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.
...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
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 ...
|
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...
| |