calling unmanaged dll function with user-defined parms

A

Angel

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
dllCalls.addVer("string1", "string2") -this is how I created the method in
the class. Now, in this static method that uses DllImport to read the dll,
I need to move "string1" and "string2" into a structure called PARMS_DIRS
since the only way the dll function can be executed is if I call it with a
parm of type PARMS_DIRS. In the example given by the company, they use this
code:

#include <stdlib.h>
#include <parms.h> // header file with PARMS_DIRS info
PARMS_DIRS parm; //the proprietary structure

void main(void)
{
...
memset(&parm,0, sizeof(parm));
strcpy(parm.type1, "100239"); //first string to be moved into struct
strcpy(parm.type2, "John Smith"); //second string to be moved into
struct
verName(&parm); //verName is the function in the DLL
...
}

After the function is executed, it fills the struct with other additional
info (parm.result1, parm.result2) which I need to process the employee.

How can I have access to this structure from within my C# application?

Thanks.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Angle,
It depends on how the structure PARMS_DIRS is declared.
You have to declare managed version of this structure.

If the structure has those two strings as in-lined strings in the structure
you should use MarshalAsAttribute with unmanaged type ByValTStr and properly
set SizeConst property.

If the structure has the strings as pointers you should use StringBuilder
type in your managed structure since you want the strings to be changed
 
A

Angel

Thanks for your response.
I tried doing that but I received a "Could not load ... from assembly
verName because the method verName has no RVA."

The original C struct looked like this:
typedef struct
{
char type1[1];
char type2[1];
struct
{
char result1[1];
char result2[1];
} foot;
char rsvd4[1];
} ZIP4_PARM;

and I converted it in Parms.cs as:

public struct ZIP4_PARM
{
char type1;
char type2;
struct foot
{
char result1;
char result2;
}
char rsvd;
]

What can I do?

Thanks.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Angel,
1. If the members are arrays of 1 character in order to have them properly
marshaled you should declare them as strings.
2. Even though in the original *result1* and *result2* are declared in a
nested structure you cannot do that in c#.

So, IMHO the corect declaration should be something like

[ StructLayout( LayoutKind.Sequential )]
struct ZIP4_PARM
{
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1)]
public string type1;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1)]
public string type2;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1)]
public string result1;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1)]
public string result2;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1)]
public string rsvd;
}

However, there are other things you should take care of.
You might need to set the CharSet property of StructLayout attribute to
control whether the characters are ASCII or UNICODE. If the methods that you
call accepts pointer to the structure you may need to declare it as a class
instead of structure.

--
HTH
B\rgds
100
Angel said:
Thanks for your response.
I tried doing that but I received a "Could not load ... from assembly
verName because the method verName has no RVA."

The original C struct looked like this:
typedef struct
{
char type1[1];
char type2[1];
struct
{
char result1[1];
char result2[1];
} foot;
char rsvd4[1];
} ZIP4_PARM;

and I converted it in Parms.cs as:

public struct ZIP4_PARM
{
char type1;
char type2;
struct foot
{
char result1;
char result2;
}
char rsvd;
]

What can I do?

Thanks.



Stoitcho Goutsev (100) said:
Hi Angle,
It depends on how the structure PARMS_DIRS is declared.
You have to declare managed version of this structure.

If the structure has those two strings as in-lined strings in the structure
you should use MarshalAsAttribute with unmanaged type ByValTStr and properly
set SizeConst property.

If the structure has the strings as pointers you should use StringBuilder
type in your managed structure since you want the strings to be changed

--
HTH
B\rgds
100

"Angel" <none> wrote in message news:[email protected]...
method
with
 
G

Guest

Hi Angel,

I think Stoitcho's reply is of much value. Is your problem resolved?

If you still have any concern, please feel free to post, we will help you.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top