473,473 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

exporting function with parm pointer to struct

I'm exporting (with DllImport) a C-style function with this syntax:

int z9indqry (4_PARM *parm);

4_PARM is a structure declared in a proprietary header file that cannot be
included in my project (due to C# limits).

What would be the other best way to do this? I would like to be able to use
the original struct because these structs have over 40 members each and
there are several more Structs similar to 4_PARM. Also, they are modified by
the company every two months so I'd need to compare and modify my C# structs
everytime they change the h structs.
I'm not going to use wrappers because the dll function calls are working
well when the parms are simple data types (int, char, string, etc...). I
just need a way to be able to call these specialized structs.

Thanks again.

P.S. - I'm pretty sure this is the last question I have regarding unmanaged
code :-))
Nov 15 '05 #1
6 1763
Angel,

If your structures are really that flexible, then I would recommend that
you not use a pointer to the struct, but rather, a pointer to void in the
declaration. This will prevent you from having to recompile, as well as
redefine the C# code (at least for the declaration).

Also, it would imply that you need a better design that is more
flexible, IMO. Changing the structure every two months is very time
consuming. It might make sense to have a keyed collection of some kind to
use.

That being said, with what you have now, you will have to declare the
structure in C#. You will want it to have the same layout in memory as the
structure in C# (remember longs in C are ints in C#, etc, etc). Also, you
will want to adorn the structure with the StructLayout attribute, passing
LayoutKind.Sequential to the constructor.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Angel" <none> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
I'm exporting (with DllImport) a C-style function with this syntax:

int z9indqry (4_PARM *parm);

4_PARM is a structure declared in a proprietary header file that cannot be
included in my project (due to C# limits).

What would be the other best way to do this? I would like to be able to use the original struct because these structs have over 40 members each and
there are several more Structs similar to 4_PARM. Also, they are modified by the company every two months so I'd need to compare and modify my C# structs everytime they change the h structs.
I'm not going to use wrappers because the dll function calls are working
well when the parms are simple data types (int, char, string, etc...). I
just need a way to be able to call these specialized structs.

Thanks again.

P.S. - I'm pretty sure this is the last question I have regarding unmanaged code :-))

Nov 15 '05 #2
Thanks for the post.

I did what you suggested but I still get "This type can not be marshaled as
a structure field" when I call the function. This is my initial code:

[DllImport("C:\\zm7\\Developm\\DLL\\ZIP4_W32.DLL")]
public static extern int z9indqry(4_PARM parm);
public static void getParms ()
{
int i;
Zip4_parm parm = new Zip4_parm();
i = z9indqry(parm); // Error: "This type can not be marshaled as a
structure field"
// IntPtr buffer = Marshal.AllocCoTaskMem( Marshal.SizeOf( parm ));
}
What can this error mean?
Also, I'm trying to delare some const vars from the initial H file (eg.
#define Z4_INVADDR 10) but adding it to the cs file that contains the
structs generates error "Expected class, delegate, enum, interface, or
struct".

Any help would be appreciated.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#9*************@TK2MSFTNGP11.phx.gbl...
Angel,

If your structures are really that flexible, then I would recommend that you not use a pointer to the struct, but rather, a pointer to void in the
declaration. This will prevent you from having to recompile, as well as
redefine the C# code (at least for the declaration).

Also, it would imply that you need a better design that is more
flexible, IMO. Changing the structure every two months is very time
consuming. It might make sense to have a keyed collection of some kind to
use.

That being said, with what you have now, you will have to declare the
structure in C#. You will want it to have the same layout in memory as the structure in C# (remember longs in C are ints in C#, etc, etc). Also, you
will want to adorn the structure with the StructLayout attribute, passing
LayoutKind.Sequential to the constructor.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Angel" <none> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
I'm exporting (with DllImport) a C-style function with this syntax:

int z9indqry (4_PARM *parm);

4_PARM is a structure declared in a proprietary header file that cannot be included in my project (due to C# limits).

What would be the other best way to do this? I would like to be able to use
the original struct because these structs have over 40 members each and
there are several more Structs similar to 4_PARM. Also, they are modified by
the company every two months so I'd need to compare and modify my C#

structs
everytime they change the h structs.
I'm not going to use wrappers because the dll function calls are working
well when the parms are simple data types (int, char, string, etc...).

I just need a way to be able to call these specialized structs.

Thanks again.

P.S. - I'm pretty sure this is the last question I have regarding

unmanaged
code :-))


Nov 15 '05 #3

Hi Angel,

Thank you for posting in the community!

I am confused by your code. In your function declaration:
public static extern int z9indqry(4_PARM parm)
It needs 4_PARM structure, but you construct a Zip4_parm, then pass it to
z9indqry.

What is "Zip4_parm"?

I think the most recommanded way is do the marshaling and declaration in C#
for your 4_PARM structure. Then use it as:
public static extern int z9indqry(ref 4_PARM parm)

If you still do not want to declare the structure. Just as Nicholas said,
you may use IntPtr, which you need to read and write the structure memory
all by yourself(Which is not recommanded)
Below is the steps:

1). Initialize a byte[] which is the same length as the structure.
2). Initilize the structure byte[] array(Do some write to this byte[] array)
3). Use Marshal.AllocCoTaskMem() to alloc a structure size in unmanaged
memory
4). Use Marshal.Copy(Byte[], Int32, IntPtr, Int32) to copy the managed
byte[] array to the unmanaged memory.
5). Use the function like this: public static extern int z9indqry(ref
IntPtr parm)

===========================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4
Thanks for your help. Instead of recreating the C-style struct as a C#
struct, I decided to use it as a class. I think that it's at least
communicating with the dll. This is how my class looks like:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi) ]
public class city
{
public city()
{
}
//[MarshalAs(UnmanagedType.LPStr)]
[MarshalAs( UnmanagedType.ByValArray, SizeConst=5 )]
public char detail_code;
public char[] zip_code;
public char[] city_key;
public char zip_class_code;
public char[] city_name;
}

The original:
typedef struct
{
char detail_code;
char zip_code[5+1];
char city_key[6+1];
char zip_class_code;
char city_name[28+1];
} CITY_REC;
When I invoke the function, I receive the error: "Can not marshal field
detail_code of type ZM7.city: Invalid managed/unmanaged type combination
(chars must be paired with U1 or U2)". At least the error specifies a field
(detail_code) inside the class, which I think may be a good sign.

What could it be?

Thanks.

"""Jeffrey Tan[MSFT]""" <v-*****@online.microsoft.com> wrote in message
news:oT**************@cpmsftngxa06.phx.gbl...

Hi Angel,

Thank you for posting in the community!

I am confused by your code. In your function declaration:
public static extern int z9indqry(4_PARM parm)
It needs 4_PARM structure, but you construct a Zip4_parm, then pass it to
z9indqry.

What is "Zip4_parm"?

I think the most recommanded way is do the marshaling and declaration in C# for your 4_PARM structure. Then use it as:
public static extern int z9indqry(ref 4_PARM parm)

If you still do not want to declare the structure. Just as Nicholas said,
you may use IntPtr, which you need to read and write the structure memory
all by yourself(Which is not recommanded)
Below is the steps:

1). Initialize a byte[] which is the same length as the structure.
2). Initilize the structure byte[] array(Do some write to this byte[] array) 3). Use Marshal.AllocCoTaskMem() to alloc a structure size in unmanaged
memory
4). Use Marshal.Copy(Byte[], Int32, IntPtr, Int32) to copy the managed
byte[] array to the unmanaged memory.
5). Use the function like this: public static extern int z9indqry(ref
IntPtr parm)

===========================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5
Hi, Angel ;)

Try the following

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi) ]
public class city
{
public city()
{
}
[MarshalAs(UnmanagedType.U1)]
public char detail_code;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=5 )]
public char[] zip_code;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=6 )]
public char[] city_key;
[MarshalAs(UnmanagedType.U1)]
public char zip_class_code;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=29 )]
public char[] city_name;
}

You should marshal 'char' as 'UnmanagedType.U1' which means C 'char' and
'char[]' as 'UnmanagedType.ByValArray, SizeConst=size' which means C 'char[size]'.

Also you may need to compile C structure with '#pragma pack push(1)' to remove data alignment.

Angel wrote:
Thanks for your help. Instead of recreating the C-style struct as a C#
struct, I decided to use it as a class. I think that it's at least
communicating with the dll. This is how my class looks like:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi) ]
public class city
{
public city()
{
}
//[MarshalAs(UnmanagedType.LPStr)]
[MarshalAs( UnmanagedType.ByValArray, SizeConst=5 )]
public char detail_code;
public char[] zip_code;
public char[] city_key;
public char zip_class_code;
public char[] city_name;
}

The original:
typedef struct
{
char detail_code;
char zip_code[5+1];
char city_key[6+1];
char zip_class_code;
char city_name[28+1];
} CITY_REC;
When I invoke the function, I receive the error: "Can not marshal field
detail_code of type ZM7.city: Invalid managed/unmanaged type combination
(chars must be paired with U1 or U2)". At least the error specifies a field
(detail_code) inside the class, which I think may be a good sign.

What could it be?

Thanks.

"""Jeffrey Tan[MSFT]""" <v-*****@online.microsoft.com> wrote in message
news:oT**************@cpmsftngxa06.phx.gbl...
Hi Angel,

Thank you for posting in the community!

I am confused by your code. In your function declaration:
public static extern int z9indqry(4_PARM parm)
It needs 4_PARM structure, but you construct a Zip4_parm, then pass it to
z9indqry.

What is "Zip4_parm"?

I think the most recommanded way is do the marshaling and declaration in


C#
for your 4_PARM structure. Then use it as:
public static extern int z9indqry(ref 4_PARM parm)

If you still do not want to declare the structure. Just as Nicholas said,
you may use IntPtr, which you need to read and write the structure memory
all by yourself(Which is not recommanded)
Below is the steps:

1). Initialize a byte[] which is the same length as the structure.
2). Initilize the structure byte[] array(Do some write to this byte[]


array)
3). Use Marshal.AllocCoTaskMem() to alloc a structure size in unmanaged
memory
4). Use Marshal.Copy(Byte[], Int32, IntPtr, Int32) to copy the managed
byte[] array to the unmanaged memory.
5). Use the function like this: public static extern int z9indqry(ref
IntPtr parm)


--
With best regards :)
Dmitry Kostenko
Nov 15 '05 #6

Hi Angel,

Please apply Dmitry's suggestion to see if resolve your problem. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #7

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

Similar topics

2
by: M | last post by:
Hello I am trying to build an Oracle PL/SQL Package. Some functions should only be visible from the package, while other functions need to be called from outside. I tried the following: --...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
4
by: Angel | last post by:
I'm trying to call a DLL function that receives as parameter a user-defined structure created by the company that made the dll. When I call the function from my main form, I call...
2
by: Angel | last post by:
I'm exporting a C-style function call with this syntax: int getDate(char *date); I'm trying to export to my c# app like this: public static extern int getDate(System.IntPtr ptr); public...
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
1
by: Saad | last post by:
Hi, I have a MC++dll, whose functions i want to use in unmanaged c++. I read that one of the ways to use the managed functions in unmanaged world is by exposing the managed api as flat api. I...
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
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...
1
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,...
1
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...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.