473,804 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InteropServices : How to map .NET data type to LPVOID

Hello,

I am attempting to create an application that uses an existing driver
interface API using C#. I have an API that looks like

F32x_Read( HANDLE Handle, LPVOID Buffer, DWORD NumBytesToRead, DWORD
*NumBytesReturn ed)

Where the following arguments are described as

Handle - Handle to the device to read as returned by SI_Open
Buffer - Address of a character buffer to be filled with read data
NumBytesToRead - Number of bytes to read from the device into the
buffer(0-64KBytes)
NumBytesReturne d - Address of a DWORD which will contain the number of bytes
actually read into the buffer

Within a class I have the DLLImport defined as

[DllImport( "SiF32xUSB.DLL" , EntryPoint = "F32x_Read" , SetLastError = true,
CharSet = CharSet.Unicode , ExactSpelling = true,
CallingConventi on = CallingConventi on.StdCall )]
private static extern unsafe
System.Int32 F32x_Read( System.UInt32 Handle,
System.IntPtr Buffer,
System.UInt32 NumBytesToRead,
System.UInt32* NumBytesReturne d
);

But I can't see to figure out how to define the public method that the
application will consume this interface. I know that I need a "fixed"
declaration so the Buffer and NumBytesReturne d are pinned, but I don't know
what .NET data type should I use for the IntPtr. I always get compile errors
if I used either string, char, byte.

There is another API that is similar to this one that takes a LPVOID Buffer
that returns a null terminated string, but doesn't tell me how many character
were copied. In addition, the buffer needs to be up to 256. Once I know the
correct data type to use will the ToString() method convert copied data into
the buffer to the correct length or do I need to perform some sort of
manipulation? Any help is greatly appreciated. Thanks

Mark
Nov 17 '05 #1
1 6722
Hello,

You should declare a byte[] array large enough to fit the data read, then
pin the array in memory by using GCHandle.Alloc, and then perform an
explicit type conversion of the obtained GCHandle to IntPtr. This will be
the IntPtr you shoud pass to the unmanaged F32x_Read function. You should
also re-declare the P/Invoke declaration like this:

[DllImport( "SiF32xUSB.DLL" , EntryPoint = "F32x_Read" , SetLastError = true,
CharSet = CharSet.Unicode , ExactSpelling = true,
CallingConventi on = CallingConventi on.StdCall )]
private static extern
System.Int32 F32x_Read( System.UInt32 Handle,
System.IntPtr Buffer,
System.UInt32 NumBytesToRead,
ref System.UInt32 NumBytesReturne d
);

(no need for unsafe).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"LongBow" <pi***@mud.pool > wrote in message
news:8i******** *************** *********@4ax.c om...
Hello,

I am attempting to create an application that uses an existing driver
interface API using C#. I have an API that looks like

F32x_Read( HANDLE Handle, LPVOID Buffer, DWORD NumBytesToRead, DWORD
*NumBytesReturn ed)

Where the following arguments are described as

Handle - Handle to the device to read as returned by SI_Open
Buffer - Address of a character buffer to be filled with read data
NumBytesToRead - Number of bytes to read from the device into the
buffer(0-64KBytes)
NumBytesReturne d - Address of a DWORD which will contain the number of
bytes
actually read into the buffer

Within a class I have the DLLImport defined as

[DllImport( "SiF32xUSB.DLL" , EntryPoint = "F32x_Read" , SetLastError =
true,
CharSet = CharSet.Unicode , ExactSpelling = true,
CallingConventi on = CallingConventi on.StdCall )]
private static extern unsafe
System.Int32 F32x_Read( System.UInt32 Handle,
System.IntPtr Buffer,
System.UInt32 NumBytesToRead,
System.UInt32* NumBytesReturne d
);

But I can't see to figure out how to define the public method that the
application will consume this interface. I know that I need a "fixed"
declaration so the Buffer and NumBytesReturne d are pinned, but I don't
know
what .NET data type should I use for the IntPtr. I always get compile
errors
if I used either string, char, byte.

There is another API that is similar to this one that takes a LPVOID
Buffer
that returns a null terminated string, but doesn't tell me how many
character
were copied. In addition, the buffer needs to be up to 256. Once I know
the
correct data type to use will the ToString() method convert copied data
into
the buffer to the correct length or do I need to perform some sort of
manipulation? Any help is greatly appreciated. Thanks

Mark


Nov 17 '05 #2

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

Similar topics

2
12656
by: greyham | last post by:
Hi. I'm a beginner to C++ so bear with me. I'm trying to pass a character array to a new thread so I thought I could pass the pointer to the character array and cast it as a LPVOID (I know nothing about LPVOIDs by the way). So I currently have: AfxBeginThread(myThread,(LPVOID) *myBuffer); My method myThread looks like: UINT myThread(LPVOID LParam) {
0
1493
by: Logan McKinley | last post by:
I need to access a function from a dll but am having problems with the conversion. the def from the documentation is: extern "C" __stdcall int tira_get_captured_data_vb ( unsigned char data, int* size ); I tried converting that to: public static extern int tira_get_captured_data_vb(ref char data, ref int size);
2
3482
by: Bonj | last post by:
hello I've got a C DLL that I've built which has got a callback function. Now I want to call it from C#. The method of using a delegate for the callback function works absolutely beautifully. However, the exported function of the Dll (that I PInvoke from c#) has an LPVOID parameter for extradata, which it passes to the callback function. How would I go about coding it if I wanted to pass a reference to a C# class through this LPVOID?...
4
1167
by: Notre Poubelle | last post by:
Hello, I have some code where I need to store a value as LPVOID. I'd like to be able to do some runtime testing about whether the thing that's pointed to is of a certain type. I've tried using RTTI's typeid and dynamic_cast functions (the latter won't compile), but they've not worked for me. I've included a short sample program that sort of shows what I'm trying to do and possibly reveals whether I'm doing something dumb. The...
7
8923
by: Toni | last post by:
Hello Everyone , I'm trying to convert an LPCSTR to LPVOID for use in DeviceIOControl. But when i use LPVOID test = (LPVOID)test_lpcstr then it doesn't work , When i use LPVOID test = "TEST" , then it works , so it must have something to do with the conversion. Any ideas ? Note: i'm very new to C++ Sorry for my poor english Edit: DeviceIoControl(driverHandle, CHANGE_PREFIX, "TEST", sizeof("TEST"), szTemp, sizeof(szTemp), &dwReturn,...
5
4638
RedSon
by: RedSon | last post by:
Hi all, HANDLE hnd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)handleArray, 0, &dwThread); ... DWORD ThreadProc(LPVOID lpParameter) {
4
9100
by: ppuniversal | last post by:
Hello, I am making a thread program, in which i call : hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)(this->runThread), this, 0, &ThreadId);
7
2887
by: Young | last post by:
This makes me very puzzled and angry.Please have a look at : ....... ....... _hSelf=::CreateWindowEx( WS_EX_ACCEPTFILES, (LPCWSTR)_className,\ (LPCWSTR)"Web Studio 2008",\ WS_OVERLAPPEDWINDOW,\ CW_USEDEFAULT,\ CW_USEDEFAULT,\
4
1716
by: George2 | last post by:
Hello everyone, My question is whether my understanding of the solution to type safe issue in CoCreateInstance is correct. There is type safe issue in CoCreateInstance, http://msdn2.microsoft.com/en-us/library/ms686615.aspx STDAPI CoCreateInstance( REFCLSID rclsid, LPUNKNOWN pUnkOuter,
0
10600
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
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
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,...
1
7638
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
5534
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
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
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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.