473,416 Members | 1,550 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,416 software developers and data experts.

problem dllImport struct array

1
Hi,

I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong.

Here is some code:


#define USERINT_FUNC __cdecl
#ifdef __cplusplus
extern "C" {
#endif
bool __cdecl CCInit();
int __cdecl CCStartScan();
int __cdeclC CCStopScan();
int __cdecl CCGetAllDevsByArray(DevDataRecord *dda, int maxDevices);
#ifdef __cplusplus
}
#endif

struct __declspec(dllexport) DevDataRecord
{
unsigned long snr; // @field unsigned long
char name[256]; // @field char[256]
int dhcp; // @field int
char ip[24]; // @field char[24]
char netmask[24]; // @field char[24]
char gateway[24]; // @field char[24]
int signature; // @field int
char targetname[256]; // @field char[256]
char id[24]; // @field char[24]
int devindex; // @field int
int devmhomeidx; // @field int
char version[24]; // @field char[24]
char model[24]; // @field char[24]
char bootloaderversion[24]; // @field char[24]
char hwrevision[24]; // @field char[24]
int devicetype; // @field int
char physicaladdress[24]; // @field char[24]
char bname[256]; // @field char[256]
unsigned long bsnr; // @field unsigned long
char bhwrevision[24]; // @field cahr[24]
int dataFlags; // @field int
};


/************************************************** *********/

This is some test code in Microsoft Visual C++ 6.0. This works fine. The function returns the right devCount and the gDdr array is filled correctly.

#define MAX_DEVICE_COUNT 100
DevDataRecord gDdr[MAX_DEVICE_COUNT];

...

memset(gDdr, 0, sizeof(gDdr) );
int devCount = CCGetAllDevsByArray(gDdr, MAX_DEVICE_COUNT);


/************************************************** *********/

Then I started a C# console application in visual studio 2008. I made a Class that calls the functions from the unmannaged dll.

class Caller
{

#region Dll Imports

[DllImport(@"C:\Temp\ChipControl.dll", SetLastError = true)]
static extern bool CCInit();
[DllImport(@"C:\Temp\ChipControl.dll", SetLastError = true)]
static extern int CCStartScan();
[DllImport(@"C:\Temp\ChipControl.dll", SetLastError = true)]
static extern int CCStopScan();
[DllImport(@"C:\Temp\ChipControl.dll", SetLastError = true)]
static extern int CCGetAllDevsByArray([Out]out DevDataRecord ptr, int maxDevices);


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DevDataRecord
{
public UInt32 snr; // @field unsigned long
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string name; // @field char[256]
public int dhcp; // @field int
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string ip; // @field char[24]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string netmask; // @field char[24]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string gateway; // @field char[24]
public int signature; // @field int
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string targetname; // @field char[256]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string id; // @field char[24]
public int devindex; // @field int
public int devmhomeidx; // @field int
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string version; // @field char[24]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string model; // @field char[24]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string bootloaderversion; // @field char[24]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string hwrevision; // @field char[24]
public int devicetype; // @field int
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
public string physicaladdress; // @field char[24]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string bname; // @field char[256]
public ulong bsnr; // @field unsigned long
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
public char[] bhwrevision; // @field char[24]
public int dataFlags; // @field int
};

#endregion

public const int MAX_DEVICE_COUNT = 100;
public DevDataRecord[] gDdr;


public Caller()
{
gDdr = new DevDataRecord[MAX_DEVICE_COUNT];
}
public void Init()
{
CCInit();
}
public void StartScan()
{
CCStartScan();
}
public void StopScan()
{
CCStopScan();
}


When I call the function like following, it returns the correct integer, but only the first element of the array is filled. This is the only way to get some result.

public int GetAllDevsByArray()
{
try
{
return CCGetAllDevsByArray(out gDdr[0], MAX_DEVICE_COUNT);
}
catch (Exception e)
{
return 0;
}
}
}

I've tried a lot of other ways that I found in several forums or sites, but none of them work.

Here are some of the other ways:

-----------------------------------------------------------------------------------

static extern int CCGetAllDevsByArray([Out]out DevDataRecord[] ptr, int maxDevices);
public int GetAllDevsByArray()
{
try
{
return CCGetAllDevsByArray(out gDdr, MAX_DEVICE_COUNT);
}
catch (Exception e)
{
return 0;
}
}
}
-------------------------------------------------------------------------------------
[DllImport(@"C:\Temp\ChipControl.dll", SetLastError = true)]
static extern int CCGetAllDevsByArray(ref DevDataRecord ptr, int maxDevices);
public int GetAllDevsByArray()
{
try
{
return CCGetAllDevsByArray(ref gDdr[0], MAX_DEVICE_COUNT);
}
catch (Exception e)
{
return 0;
}
}
-------------------------------------------------------------------------------------
[DllImport(@"C:\Temp\ChipControl.dll", SetLastError = true)]
static extern int CCGetAllDevsByArray(ref IntrPtr data, int maxDevices);
public int GetAllDevsByArray(int maxDevices)
{
try
{
unsafe
{
fixed (IntPtr ptr = gDdr)
{
return CCGetAllDevsByArray(ptr, maxDevices);
}
}
}
catch (Exception e)
{
return 0;
}
}
-------------------------------------------------------------------------------------
[DllImport(@"C:\Temp\ChipControl.dll", SetLastError = true)]
static extern int CCGetAllDevsByArray(ref Intptr data, int maxDevices);
public int GetAllDevsByArray(int maxDevices)
{
try
{
int arrayLen = gDdr.Length;
int structSize = Marshal.SizeOf(typeof(DevDataRecord));
IntPtr ptr = Marshal.AllocCoTaskMem(arrayLen * structSize);
for (int i = 0; i < arrayLen; i++)
{
Marshal.StructureToPtr(gDdr[i], (IntPtr)(ptr.ToInt32() + i * structSize), false);
}
int nrDevs = CCGetAllDevsByArray(ref ptr, maxDevices);
for (int i = 0; i < gDdr.Length; i++)
{
gDdr[i] = (DevDataRecord)Marshal.PtrToStructure(ptr, typeof(DevDataRecord));
ptr = (IntPtr)((int)ptr + Marshal.SizeOf(typeof(DevDataRecord)));
}
return nrDevs;
}
catch (Exception e)
{
return 0;
}
}

Is there someone who can help me and tell me what I'm doing wrong?
Many thanks,

Elke
Mar 25 '09 #1
1 5769
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Mar 25 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Schorschi | last post by:
Can someone please point out why I am getting an 87 error? I am sure it is obvious, but I am new to C# and seem to be having a lot of stress understanding managed versus unmanaged code when API...
1
by: Keltus | last post by:
Hi, I am trying to write a C# wrapper class for the famous mp3 decoder mpg123. I have gotten the win32 .dll version of it, and have a sample C program that uses the .dll which I think is pretty...
15
by: Jim | last post by:
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the windows console the...
4
by: Dirk Reske | last post by:
Hello, I've importet following struct typedef struct { DWORD cbStruct; DWORD fdwStatus; DWORD_PTR dwUser; LPBYTE pbSrc; DWORD cbSrcLength; DWORD cbSrcLengthUsed;
3
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got...
3
by: Mohammad-Reza | last post by:
Hi everybody I want to shutdown my computer with my program. I searched in MSDN to find a .NET method that can do that but did'nt find anything but ExitWindowsEx Api. I write following codes to...
1
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...
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
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.