473,396 Members | 1,760 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.

Sending pointer to struct to C DLL

Leo
Hello,

I have a C dll with a method signature of:

int activate(datastruct *data)

where datastruct is defined as:

typedef struct datastruct {
long result;
unsigned char data[1024];
char id[20];
} datastruct;

In C#, I have a small test class defining a struct and using the
method. The small class is this:

using System;
using System.Runtime.InteropServices;

namespace DLLTest
{
[StructLayout(LayoutKind.Sequential)]
public struct datastruct
{
public long result;
public char [] data;
public char [] id;
};

public class Tester
{
[DllImport(@"C:\DSDLL.DLL", CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall, ThrowOnUnmappableChar =
true)]
public static extern int activate(???);

[STAThread]
public static void Main(String [] args)
{
datastruct ds = new datastruct();

ds.result = 0;
ds.data = new char[1024];
ds.id = new char[32];

int i = Tester.activate(<pointer to ds???>); }
}
}

The C# code is being rewritten from C++ and the C++ code works just
fine when passing the pointer to activate(). Unfortunately I don't
have the luxury of changing the C DLL.

The C DLL should be filling ds.data with chars and ds.result with a
value, which it does in the C++ version [its an old DLL, been around
for years].

I have tried a variety of mechanisms to fill in the question marks,
all met with varying degrees of failure ranging from compiler errors
to runtime exceptions in the receiving DLL. I must be overlooking
something simple but I cannot see what it is. Maybe this isn't
possible?

I've checked the P/Invoke section and examples in MSDN (VS.NET 2003)
and the many prior postings of similar questions in this group but
have not been successful in applying them to this case.

Would someone mind reviewing the above and pointing me in the right
direction?

I appreciate any suggestions.

Thank you,
-L
Nov 16 '05 #1
4 2358
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
struct datastruct {
long result;
[MarshalAs(UnmanagedType.ByValTStr ,SizeConst=1024)]
string data;
[MarshalAs(UnmanagedType.ByValTStr ,SizeConst=20)]
string char;
};
[DllImport("C:\\DSDLL.DLL", SetLastError=true)]
public static extern int activate(ref datastruct ds);
--
kevin aubuchon
www.aubuchon-design.com
"Leo" <LR***@LYCOS.COM> wrote in message
news:b0**************************@posting.google.c om...
Hello,

I have a C dll with a method signature of:

int activate(datastruct *data)

where datastruct is defined as:

typedef struct datastruct {
long result;
unsigned char data[1024];
char id[20];
} datastruct;

In C#, I have a small test class defining a struct and using the
method. The small class is this:

using System;
using System.Runtime.InteropServices;

namespace DLLTest
{
[StructLayout(LayoutKind.Sequential)]
public struct datastruct
{
public long result;
public char [] data;
public char [] id;
};

public class Tester
{
[DllImport(@"C:\DSDLL.DLL", CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall, ThrowOnUnmappableChar =
true)]
public static extern int activate(???);

[STAThread]
public static void Main(String [] args)
{
datastruct ds = new datastruct();

ds.result = 0;
ds.data = new char[1024];
ds.id = new char[32];

int i = Tester.activate(<pointer to ds???>); }
}
}

The C# code is being rewritten from C++ and the C++ code works just
fine when passing the pointer to activate(). Unfortunately I don't
have the luxury of changing the C DLL.

The C DLL should be filling ds.data with chars and ds.result with a
value, which it does in the C++ version [its an old DLL, been around
for years].

I have tried a variety of mechanisms to fill in the question marks,
all met with varying degrees of failure ranging from compiler errors
to runtime exceptions in the receiving DLL. I must be overlooking
something simple but I cannot see what it is. Maybe this isn't
possible?

I've checked the P/Invoke section and examples in MSDN (VS.NET 2003)
and the many prior postings of similar questions in this group but
have not been successful in applying them to this case.

Would someone mind reviewing the above and pointing me in the right
direction?

I appreciate any suggestions.

Thank you,
-L

Nov 16 '05 #2
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
struct datastruct {
long result;
[MarshalAs(UnmanagedType.ByValTStr ,SizeConst=1024)]
string data;
[MarshalAs(UnmanagedType.ByValTStr ,SizeConst=20)]
string char;
};
[DllImport("C:\\DSDLL.DLL", SetLastError=true)]
public static extern int activate(ref datastruct ds);

--
kevin aubuchon
www.aubuchon-design.com
"Leo" <LR***@LYCOS.COM> wrote in message
news:b0**************************@posting.google.c om...
Hello,

I have a C dll with a method signature of:

int activate(datastruct *data)

where datastruct is defined as:

typedef struct datastruct {
long result;
unsigned char data[1024];
char id[20];
} datastruct;

In C#, I have a small test class defining a struct and using the
method. The small class is this:

using System;
using System.Runtime.InteropServices;

namespace DLLTest
{
[StructLayout(LayoutKind.Sequential)]
public struct datastruct
{
public long result;
public char [] data;
public char [] id;
};

public class Tester
{
[DllImport(@"C:\DSDLL.DLL", CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall, ThrowOnUnmappableChar =
true)]
public static extern int activate(???);

[STAThread]
public static void Main(String [] args)
{
datastruct ds = new datastruct();

ds.result = 0;
ds.data = new char[1024];
ds.id = new char[32];

int i = Tester.activate(<pointer to ds???>); }
}
}

The C# code is being rewritten from C++ and the C++ code works just
fine when passing the pointer to activate(). Unfortunately I don't
have the luxury of changing the C DLL.

The C DLL should be filling ds.data with chars and ds.result with a
value, which it does in the C++ version [its an old DLL, been around
for years].

I have tried a variety of mechanisms to fill in the question marks,
all met with varying degrees of failure ranging from compiler errors
to runtime exceptions in the receiving DLL. I must be overlooking
something simple but I cannot see what it is. Maybe this isn't
possible?

I've checked the P/Invoke section and examples in MSDN (VS.NET 2003)
and the many prior postings of similar questions in this group but
have not been successful in applying them to this case.

Would someone mind reviewing the above and pointing me in the right
direction?

I appreciate any suggestions.

Thank you,
-L

Nov 16 '05 #3
This is not correct, long is 64 bit in C#, 32 bit in C.
So, long result;
should look like:
int result;

Willy.
"Kevin Aubuchon" <ke************@charter.net> wrote in message
news:eq***************@fe07.lga...
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
struct datastruct {
long result;
[MarshalAs(UnmanagedType.ByValTStr ,SizeConst=1024)]
string data;
[MarshalAs(UnmanagedType.ByValTStr ,SizeConst=20)]
string char;
};
[DllImport("C:\\DSDLL.DLL", SetLastError=true)]
public static extern int activate(ref datastruct ds);

--
kevin aubuchon
www.aubuchon-design.com
"Leo" <LR***@LYCOS.COM> wrote in message
news:b0**************************@posting.google.c om...
Hello,

I have a C dll with a method signature of:

int activate(datastruct *data)

where datastruct is defined as:

typedef struct datastruct {
long result;
unsigned char data[1024];
char id[20];
} datastruct;

In C#, I have a small test class defining a struct and using the
method. The small class is this:

using System;
using System.Runtime.InteropServices;

namespace DLLTest
{
[StructLayout(LayoutKind.Sequential)]
public struct datastruct
{
public long result;
public char [] data;
public char [] id;
};

public class Tester
{
[DllImport(@"C:\DSDLL.DLL", CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall, ThrowOnUnmappableChar =
true)]
public static extern int activate(???);

[STAThread]
public static void Main(String [] args)
{
datastruct ds = new datastruct();

ds.result = 0;
ds.data = new char[1024];
ds.id = new char[32];

int i = Tester.activate(<pointer to ds???>); }
}
}

The C# code is being rewritten from C++ and the C++ code works just
fine when passing the pointer to activate(). Unfortunately I don't
have the luxury of changing the C DLL.

The C DLL should be filling ds.data with chars and ds.result with a
value, which it does in the C++ version [its an old DLL, been around
for years].

I have tried a variety of mechanisms to fill in the question marks,
all met with varying degrees of failure ranging from compiler errors
to runtime exceptions in the receiving DLL. I must be overlooking
something simple but I cannot see what it is. Maybe this isn't
possible?

I've checked the P/Invoke section and examples in MSDN (VS.NET 2003)
and the many prior postings of similar questions in this group but
have not been successful in applying them to this case.

Would someone mind reviewing the above and pointing me in the right
direction?

I appreciate any suggestions.

Thank you,
-L


Nov 16 '05 #4
Leo
Kevin, Willy,

Thank you both very much for your assistance -- I appreciate your time
and the answer that got me going again!

Thank you,
-L

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message news:<Oy**************@TK2MSFTNGP10.phx.gbl>...
This is not correct, long is 64 bit in C#, 32 bit in C.
So, long result;
should look like:
int result;

Willy.
"Kevin Aubuchon" <ke************@charter.net> wrote in message
news:eq***************@fe07.lga...
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
struct datastruct {
long result;
[MarshalAs(UnmanagedType.ByValTStr ,SizeConst=1024)]
string data;
[MarshalAs(UnmanagedType.ByValTStr ,SizeConst=20)]
string char;
};
[DllImport("C:\\DSDLL.DLL", SetLastError=true)]
public static extern int activate(ref datastruct ds);

--
kevin aubuchon
www.aubuchon-design.com
"Leo" <LR***@LYCOS.COM> wrote in message
news:b0**************************@posting.google.c om...
Hello,

I have a C dll with a method signature of:

int activate(datastruct *data)

where datastruct is defined as:

typedef struct datastruct {
long result;
unsigned char data[1024];
char id[20];
} datastruct;

In C#, I have a small test class defining a struct and using the
method. The small class is this:

using System;
using System.Runtime.InteropServices;

namespace DLLTest
{
[StructLayout(LayoutKind.Sequential)]
public struct datastruct
{
public long result;
public char [] data;
public char [] id;
};

public class Tester
{
[DllImport(@"C:\DSDLL.DLL", CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall, ThrowOnUnmappableChar =
true)]
public static extern int activate(???);

[STAThread]
public static void Main(String [] args)
{
datastruct ds = new datastruct();

ds.result = 0;
ds.data = new char[1024];
ds.id = new char[32];

int i = Tester.activate(<pointer to ds???>); }
}
}

The C# code is being rewritten from C++ and the C++ code works just
fine when passing the pointer to activate(). Unfortunately I don't
have the luxury of changing the C DLL.

The C DLL should be filling ds.data with chars and ds.result with a
value, which it does in the C++ version [its an old DLL, been around
for years].

I have tried a variety of mechanisms to fill in the question marks,
all met with varying degrees of failure ranging from compiler errors
to runtime exceptions in the receiving DLL. I must be overlooking
something simple but I cannot see what it is. Maybe this isn't
possible?

I've checked the P/Invoke section and examples in MSDN (VS.NET 2003)
and the many prior postings of similar questions in this group but
have not been successful in applying them to this case.

Would someone mind reviewing the above and pointing me in the right
direction?

I appreciate any suggestions.

Thank you,
-L


Nov 16 '05 #5

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

Similar topics

3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
2
by: volker_nitschke | last post by:
Hi, I want to develop a program, that transmits several classes between two processes. It should be built after the guidelines of the iso/osi-reference model and work with a shared memory. At...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.