473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Yet another marshalling question

Hello All,
I am very new to C# since my job consist to design/develop CE drivers but a
client wants to use my C API into a .NET envrironment.

I have 2 functions I would like to get help with.

1- BOOL WriteDataSyncCard (unsigned short Address, unsigned char* pBuffer,
unsigned char* Length);
pBuffer is allocated outside the function and will not modified
Length is a 1 byte length value and can modified in the function with the
actual written number of bytes.

2- BOOL ReadDataSyncCard (unsigned short Address, unsigned char* pBuffer,
unsigned char* Length);
pBuffer is allocated outside the function and will be updated with the data
read from the card
Length is a 1 byte length value and can modified in the function with the
actual read number of bytes.

I have look aroud and found a few informations on what to be done. Here is
what I have done for now.

[DllImport("SyncContactCardAPI.dll")]
private static extern int WriteDataSyncCard(Int16 Address, byte[] pBuffer,
IntPtr Length);

public static int WriteData(Int16 Address, byte[] pBuffer, IntPtr Length)
{
//Do I need to do something with the parameters here since they are only
a pass-throught??
int Status = WriteDataSyncCard(Address, pBuffer, Length);
return Status;
}

[DllImport("SyncContactCardAPI.dll")]
private static extern int ReadDataSyncCard(Int16 Address, IntPtr pBuffer,
IntPtr Length);

public static int ReadData(Int16 Address, out byte[] pBuffer, IntPtr Length)
{
int Offset = 0;
byte BufferSize = //How do I get the 1 byte value pointed by Length??;

IntPtr Data = MemoryManager.AllocHeap((int)BufferSize);

int Status = ReadDataSyncCard(Address, Data, Length);

//Getting actual read size
BufferSize = //How do I get the 1 byte value pointed by Length??;

pBuffer = new byte[io_ProfileSize];
for (uint i = 0; i < BufferSize; i++)
{
pBuffer[i] = Marshal.ReadByte(Data, Offset);
Offset++;
}

MemoryManager.FreeHeap(Data);

return Status;
}

Thank you very much!

Keaven
Jan 9 '08 #1
1 1367
Keaven,

You don't need to declare the Length parameters as IntPtrs and then
allocate memory for them. Just declare them as ref byte and then pass them
as such and it will work just fine.

Also, are the unsigned chars strings? If it is, you can declare them as
strings, and pass them directly, instead of doing the conversion yourself.
Just make sure to attach the MarshalAs attribute, setting the UnmanagedType
to UnmanagedType.LPStr.

Finally, if you are using unsigned shorts, then you should use the
ushort type. Int16 is signed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Keaven Pineau" <ke****@no.spam.comwrote in message
news:Oy**************@TK2MSFTNGP05.phx.gbl...
Hello All,
I am very new to C# since my job consist to design/develop CE drivers but
a client wants to use my C API into a .NET envrironment.

I have 2 functions I would like to get help with.

1- BOOL WriteDataSyncCard (unsigned short Address, unsigned char*
pBuffer, unsigned char* Length);
pBuffer is allocated outside the function and will not modified
Length is a 1 byte length value and can modified in the function with the
actual written number of bytes.

2- BOOL ReadDataSyncCard (unsigned short Address, unsigned char* pBuffer,
unsigned char* Length);
pBuffer is allocated outside the function and will be updated with the
data read from the card
Length is a 1 byte length value and can modified in the function with the
actual read number of bytes.

I have look aroud and found a few informations on what to be done. Here
is what I have done for now.

[DllImport("SyncContactCardAPI.dll")]
private static extern int WriteDataSyncCard(Int16 Address, byte[] pBuffer,
IntPtr Length);

public static int WriteData(Int16 Address, byte[] pBuffer, IntPtr Length)
{
//Do I need to do something with the parameters here since they are
only a pass-throught??
int Status = WriteDataSyncCard(Address, pBuffer, Length);
return Status;
}

[DllImport("SyncContactCardAPI.dll")]
private static extern int ReadDataSyncCard(Int16 Address, IntPtr pBuffer,
IntPtr Length);

public static int ReadData(Int16 Address, out byte[] pBuffer, IntPtr
Length)
{
int Offset = 0;
byte BufferSize = //How do I get the 1 byte value pointed by Length??;

IntPtr Data = MemoryManager.AllocHeap((int)BufferSize);

int Status = ReadDataSyncCard(Address, Data, Length);

//Getting actual read size
BufferSize = //How do I get the 1 byte value pointed by Length??;

pBuffer = new byte[io_ProfileSize];
for (uint i = 0; i < BufferSize; i++)
{
pBuffer[i] = Marshal.ReadByte(Data, Offset);
Offset++;
}

MemoryManager.FreeHeap(Data);

return Status;
}

Thank you very much!

Keaven

Jan 9 '08 #2

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

Similar topics

3
by: PHil Coveney | last post by:
Hello, I am having difficulty marshalling structures when calling DeviceIoControl. I am importing this Win32 function as static extern int DeviceIoControl (int hDevice, int...
4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
1
by: MuZZy | last post by:
Hello, I am probably facing a sort of language barrier, but i can't really get what 'marshalling' is in terms of .NET Is it a kind of type casting? And when do you use it? Thank you, Andrey
2
by: Rookie | last post by:
Hi, I had a question on DllImport. On importing a function from a VC++ dll using DllImport (to a C# program), the function argument data types and the return types may be of a type that is not...
2
by: BartMan | last post by:
Greetings, When working with managed c++, do you have to do anything special when going from simple types from managed to unmanaged and vice versa. Or is marshalling handled automatically for...
1
by: GianPiero Andreis | last post by:
Hello All, let me pose a simple question about combobox and the CB_INSERTSTRING message. Suppose for instance that I already have a combobox handle, how can I declare and use the SendMessage...
10
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
5
by: PickwickBob3 | last post by:
I am trying to obtain a list of HID devices and am trying to use UINT GetRawInputDeviceList( PRAWINPUTDEVICELIST pRawInputDeviceList, PUINT puiNumDevices, UINT cbSize); a USER32.dll. but...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.