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

Home Posts Topics Members FAQ

calling dll function with struct like this (I think I may have possible solution)

Hello again (and again, and again...)
I think I'm getting closer to solving my initial problem of calling
unmanaged code. I managed to call the functions with user-defined structs
w/o getting any run-time exceptions. The problem is that I'm receiving a
Failure return (it returns 1 = "Failure"). I have a feeling it may be that
the function's not reading the struct correctly.

This time I decided to display all the code (it's very short). The original
struct declared in the .h looks like this:

typedef struct
{
char detail_code; /* copyright detail code */
char zip_code[5+1]; /* zip code */
char city_key[6+1]; /* city/state key */
} CITY_REC;

The original function syntax is:
int z4ctynxtSTD (CITY_REC *city_rec); //STD for stdcall calling
convention

The struct I converted it to: (in another cs file which only has this
struct):

namespace ZM
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CITY_REC
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string detail_code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=6)]
public string zip_code;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=7)]
public string city_key;
}
and the function call i converted to:
[DllImport("C:\\zm7\\Developm\\DLL\\W32.DLL")]
public static extern int z4ctynxtSTD(CITY_REC city); //I used ref but it
didn't work either
public static void getCity()
{
ZIP4_PARM_class zip4 = new ZIP4_PARM_class();
CITY_REC city = new CITY_REC();
int i = z4open();
i = z4ctynxtSTD(city); //I used ref but it didn't work either
MessageBox.Show ("result: " + i.ToString()); // it returns 1, which
is "Failure"
z4close();
}

Am I declaring the struct correctly and am I sending it as parameter
correctly? I'm pretty sure I'm almost done.

Thanks again (and again, and again, and again... :-)) )
Nov 15 '05 #1
3 2429

Hi Angel,

Thank you for posting in the community!

Based on my understanding, you P/invoke your unmanaged api, but failed.

================================
I think the problem should be the marshal encoding problem.

Please try to use UnmanagedType.LPStr to replace UnmanagedType.ByValTStr to
see if resolved your problem. Also, you should apply "ref" in your function
parameter, like this: public static extern int z4ctynxtSTD(ref CITY_REC
city)

If the problem still exist, I think you may first use pure C++ to invoke
this function to see if the problem is related to P/invoke

===============================
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 #2
Thanks for your help.
The company that wrote the DLL sent me some code that calls the api from C#
and it works so I don't think it has anything to do with p/invoke.. I'm
trying it with another function (and another struct) and that's where I get
the problem.

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:NM**************@cpmsftngxa06.phx.gbl...

Hi Angel,

Thank you for posting in the community!

Based on my understanding, you P/invoke your unmanaged api, but failed.

================================
I think the problem should be the marshal encoding problem.

Please try to use UnmanagedType.LPStr to replace UnmanagedType.ByValTStr to see if resolved your problem. Also, you should apply "ref" in your function parameter, like this: public static extern int z4ctynxtSTD(ref CITY_REC
city)

If the problem still exist, I think you may first use pure C++ to invoke
this function to see if the problem is related to P/invoke

===============================
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 #3
Hi Angel,

Thanks very much for your feedback.

In C# P/invoke, marshal C-style structure as C# structure almost has not
different as C# class. The different is that, C# structure is a value type,
while a C# class is a reference type. So parameter like "ref struct" can be
replaced with "class". This is the only difference.

So I think the problem has nothing to do with whether use class or
structure, the problem should still be the marshaling.

In another post of you "exporting function with parm pointer to struct", a
community member has provided you a suitable solution for marshal as class.

Note: please use that class in your code like this: z4ctynxtSTD(city inst),
do not add "ref" in the parameter.

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

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

Similar topics

6
by: jchao123 | last post by:
Dear All, I have an MDB file (Access 2000/XP) which contains generic routines I use in various apps (eg, API calls, File access classes etc). I have compiled into an MDE file which I reference...
2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
16
by: Saroj | last post by:
Is there an way for a function to know who called him ?
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
21
by: Joakim Hove | last post by:
Hello, I have implemented a small library with a function a datatype to manage temporary storage, and handle out correctly casted storage. The function to get a double pointer is for instance: ...
2
by: Joerg Battermann | last post by:
Hello there, I have a quick question: When calling a function B from within a function A, is it possible get information about the calling function A (e.g. name, values passed over etc) from...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I...
4
by: Alef.Veld | last post by:
Some reassurance here please. This crashes on my computer. When you're in a while loop, maybe a very tight one, is it in any case possible that the free in this case would occur before the first...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
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
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,...
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
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.