c dll to c#

G

Guest

Hi everyone.
I have a .dll that i want to call from a c# source. But i can't figure out a
way...
Here are the .dll's input variables and the definition.

extern "C" void _stdcall MyFunc(unsigned char var1[100], int length,
unsigned long *arrFinal)

And I want to call it by [DllImport(path,EntryPoint=MyFunc)]
public static extern void MyFunc(here comes the variables)

but i cannot figure out the replacements for unsigned char and unsigned long
* types in c#.

Can you help me? Thanks.
 
G

Guest

Hello,

Try to use System.Byte[100] for unsigned char[100] and System.UInt64 for
unsigned long.

Mihaly
 
W

Willy Denoyette [MVP]

Not at all, UInt64 is 64 bit, a long in C is 32 bit!

Willy.

| Hello,
|
| Try to use System.Byte[100] for unsigned char[100] and System.UInt64 for
| unsigned long.
|
| Mihaly
|
| "Arda Zuber" wrote:
|
| > Hi everyone.
| > I have a .dll that i want to call from a c# source. But i can't figure
out a
| > way...
| > Here are the .dll's input variables and the definition.
| >
| > extern "C" void _stdcall MyFunc(unsigned char var1[100], int length,
| > unsigned long *arrFinal)
| >
| > And I want to call it by [DllImport(path,EntryPoint=MyFunc)]
| > public static extern void MyFunc(here comes the variables)
| >
| > but i cannot figure out the replacements for unsigned char and unsigned
long
| > * types in c#.
| >
| > Can you help me? Thanks.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

That depends.

The size of an int is defined as the word size of the CPU. A long is
only defined to be at least the size of an int.
 
G

Guest

your answers are very helpful, thanks a lot but i think one subject goes
unmentioned.

What about the pointer? I mean how can i replace "unsigned long *arrFinal"

Note that arrFinal is a pointer. What is the pointer conversion in c#. So
can you tell me the what will be the variables?

I mean "MyFunc(Byte var1,int length,??????)"

What is the third variable?In terms of pointer?

Thanks a lot.
 
B

Boaz Ben-Porat

Hi Arda

It is not competely clear, but I belive that 'arrFinal' is an array of
unsigned long that is allocated/declared before the call to MyFunc(), with
the size = 'length' variable.

Try this (Not testet)

Byte[] var1 = new Byte[100];
int length = <some value>;
UInt32[] arrFinal = new UInt32[length];

// Call
MyFunc(var1, length, ref arrFinal ); // NOTE: arrFinal is passed by ref.

Hope it helps

Boaz Ben-Porat
Milestone Systems
 
S

sragner

Hi!

Could you help me?

How should I call these C function from C# with DLLImport

_Vsl_Api(int,vslNewStream,(VSLStreamStatePtr* , int , unsigned int ))
_vsl_api(int,vslnewstream,(VSLStreamStatePtr* , int *, unsigned int *))
_VSL_API(int,VSLNEWSTREAM,(VSLStreamStatePtr* , int *, unsigned int *))

Where

#define _Vsl_Api(rtype,name,arg) extern rtype __cdecl name arg;
#define _vsl_api(rtype,name,arg) extern rtype __cdecl name
arg;
#define _VSL_API(rtype,name,arg) extern rtype __cdecl name
arg;

and

typedef void* VSLStreamStatePtr;

This is in Intel MKL library's random number generators library (VSL).
BLAS and LAPACK calls were OK while I found a sample ( dnAnalytics )
but I cant solve this.

Thanks,

Laszlo
 
W

Willy Denoyette [MVP]

Well, the OP is talking about .NET PInvoke interop on Windows platforms
right? Well, in that case a long in C is 32 bit (or could you name me a
Windows C compiler that has 64 bit longs?) and the PInvoke layer will pass
an UInt64 as a "long long" (64 bit integer) which is not what is expected.



Willy.


| That depends.
|
| The size of an int is defined as the word size of the CPU. A long is
| only defined to be at least the size of an int.
|
| Willy Denoyette [MVP] wrote:
| > Not at all, UInt64 is 64 bit, a long in C is 32 bit!
| >
| > Willy.
| >
| > | > | Hello,
| > |
| > | Try to use System.Byte[100] for unsigned char[100] and System.UInt64
for
| > | unsigned long.
| > |
| > | Mihaly
| > |
| > | "Arda Zuber" wrote:
| > |
| > | > Hi everyone.
| > | > I have a .dll that i want to call from a c# source. But i can't
figure
| > out a
| > | > way...
| > | > Here are the .dll's input variables and the definition.
| > | >
| > | > extern "C" void _stdcall MyFunc(unsigned char var1[100], int length,
| > | > unsigned long *arrFinal)
| > | >
| > | > And I want to call it by [DllImport(path,EntryPoint=MyFunc)]
| > | > public static extern void MyFunc(here comes the variables)
| > | >
| > | > but i cannot figure out the replacements for unsigned char and
unsigned
| > long
| > | > * types in c#.
| > | >
| > | > Can you help me? Thanks.
| >
| >
 
W

Willy Denoyette [MVP]

Not really, just name me a C compiler that targets the Microsoft Windows
platform who has a 64 bit "built-in" long type.
Anyway, .NET interop (both PInvoke and COM) cannot rely on a 'vague'
definition of fundamental types like integers to build systems that have to
pass such types across module boundaries, that's why there is an interop
mapping defined (search MSDN for details on Marshaling arguments, this
mapping for an int and a long looks like:
wtypes.h INT = C++ int = C++/CLI int = CLR Int32
wtypes.h LONG = C++ long = C++/CLI long = CLR Int32
See, both are mapped to In32 and the value is passed "as is". If you happen
to interop with a library that assumes a long to be 64 bit, you better know
it or you are in trouble.

Willy.


| Yes, for that specific C compiler, that is true.
|
| Willy Denoyette [MVP] wrote:
| > Well, the OP is talking about .NET PInvoke interop on Windows platforms
| > right? Well, in that case a long in C is 32 bit (or could you name me a
| > Windows C compiler that has 64 bit longs?) and the PInvoke layer will
pass
| > an UInt64 as a "long long" (64 bit integer) which is not what is
expected.
| >
| >
| >
| > Willy.
| >
| >
| > | > | That depends.
| > |
| > | The size of an int is defined as the word size of the CPU. A long is
| > | only defined to be at least the size of an int.
| > |
| > | Willy Denoyette [MVP] wrote:
| > | > Not at all, UInt64 is 64 bit, a long in C is 32 bit!
| > | >
| > | > Willy.
| > | >
| > | > | > | > | Hello,
| > | > |
| > | > | Try to use System.Byte[100] for unsigned char[100] and
System.UInt64
| > for
| > | > | unsigned long.
| > | > |
| > | > | Mihaly
| > | > |
| > | > | "Arda Zuber" wrote:
| > | > |
| > | > | > Hi everyone.
| > | > | > I have a .dll that i want to call from a c# source. But i can't
| > figure
| > | > out a
| > | > | > way...
| > | > | > Here are the .dll's input variables and the definition.
| > | > | >
| > | > | > extern "C" void _stdcall MyFunc(unsigned char var1[100], int
length,
| > | > | > unsigned long *arrFinal)
| > | > | >
| > | > | > And I want to call it by [DllImport(path,EntryPoint=MyFunc)]
| > | > | > public static extern void MyFunc(here comes the variables)
| > | > | >
| > | > | > but i cannot figure out the replacements for unsigned char and
| > unsigned
| > | > long
| > | > | > * types in c#.
| > | > | >
| > | > | > Can you help me? Thanks.
| > | >
| > | >
| >
| >
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Willy said:
Not really,

Yes, really.
just name me a C compiler that targets the Microsoft Windows
platform who has a 64 bit "built-in" long type.

Why? It doesn't matter if I know of one, or if one exists, the
definitions of the data types are still the same.
Anyway, .NET interop (both PInvoke and COM) cannot rely on a 'vague'
definition of fundamental types like integers to build systems that have to
pass such types across module boundaries,

Of course not. It relies on the sizes defined in the specific compiler used.
that's why there is an interop
mapping defined (search MSDN for details on Marshaling arguments, this
mapping for an int and a long looks like:
wtypes.h INT = C++ int = C++/CLI int = CLR Int32
wtypes.h LONG = C++ long = C++/CLI long = CLR Int32
See, both are mapped to In32 and the value is passed "as is". If you happen
to interop with a library that assumes a long to be 64 bit, you better know
it or you are in trouble.

Willy.


| Yes, for that specific C compiler, that is true.
|
| Willy Denoyette [MVP] wrote:
| > Well, the OP is talking about .NET PInvoke interop on Windows platforms
| > right? Well, in that case a long in C is 32 bit (or could you name me a
| > Windows C compiler that has 64 bit longs?) and the PInvoke layer will
pass
| > an UInt64 as a "long long" (64 bit integer) which is not what is
expected.
| >
| >
| >
| > Willy.
| >
| >
| > | > | That depends.
| > |
| > | The size of an int is defined as the word size of the CPU. A long is
| > | only defined to be at least the size of an int.
| > |
| > | Willy Denoyette [MVP] wrote:
| > | > Not at all, UInt64 is 64 bit, a long in C is 32 bit!
| > | >
| > | > Willy.
| > | >
| > | > | > | > | Hello,
| > | > |
| > | > | Try to use System.Byte[100] for unsigned char[100] and
System.UInt64
| > for
| > | > | unsigned long.
| > | > |
| > | > | Mihaly
| > | > |
| > | > | "Arda Zuber" wrote:
| > | > |
| > | > | > Hi everyone.
| > | > | > I have a .dll that i want to call from a c# source. But i can't
| > figure
| > | > out a
| > | > | > way...
| > | > | > Here are the .dll's input variables and the definition.
| > | > | >
| > | > | > extern "C" void _stdcall MyFunc(unsigned char var1[100], int
length,
| > | > | > unsigned long *arrFinal)
| > | > | >
| > | > | > And I want to call it by [DllImport(path,EntryPoint=MyFunc)]
| > | > | > public static extern void MyFunc(here comes the variables)
| > | > | >
| > | > | > but i cannot figure out the replacements for unsigned char and
| > unsigned
| > | > long
| > | > | > * types in c#.
| > | > | >
| > | > | > Can you help me? Thanks.
| > | >
| > | >
| >
| >
 
W

Willy Denoyette [MVP]

| Willy Denoyette [MVP] wrote:
| > Not really,
|
| Yes, really.
|

Sorry, but we will have to agree to disagree.

| > just name me a C compiler that targets the Microsoft Windows
| > platform who has a 64 bit "built-in" long type.
|
| Why? It doesn't matter if I know of one, or if one exists, the
| definitions of the data types are still the same.
|

We are not discussing the definitions of a language, we are talking about
interop, that is pass values across managed/unmanaged module boundaries.

| > Anyway, .NET interop (both PInvoke and COM) cannot rely on a 'vague'
| > definition of fundamental types like integers to build systems that have
to
| > pass such types across module boundaries,
|
| Of course not. It relies on the sizes defined in the specific compiler
used.
|

PInvoke doesn't and can't rely on the type or version of the native compiler
used. What's important for you, is to consider the interop mapping, and
here, the documented behavior is that a long type (in C/C++) is mapped to a
Int32 (just like an int), MS (safely) assumed this to be valid because all C
compilers targetting Windows use 32 bit ints and longs, that's why I can
'safely' say to the OP, a "long" in C is an "Int32" in .NET. If the module
was built with a 'deriving' compiler (it doesn't have to be a C compiler),
you better know it before you start calling into that module.
Things are more consistent when talking about COM interop marshaling (or
user marshaling), here it's the MIDL who rules, and MIDL defines an int and
a long to be 32 bit, no confusion possible, unless another tool is used to
define the interface which derives from the MIDL definitions, but also here
AFAIK there isn't such a tool.


| > that's why there is an interop
| > mapping defined (search MSDN for details on Marshaling arguments, this
| > mapping for an int and a long looks like:
| > wtypes.h INT = C++ int = C++/CLI int = CLR Int32
| > wtypes.h LONG = C++ long = C++/CLI long = CLR Int32
| > See, both are mapped to In32 and the value is passed "as is". If you
happen
| > to interop with a library that assumes a long to be 64 bit, you better
know
| > it or you are in trouble.
| >
| > Willy.
| >
| >
| > | > | Yes, for that specific C compiler, that is true.
| > |
| > | Willy Denoyette [MVP] wrote:
| > | > Well, the OP is talking about .NET PInvoke interop on Windows
platforms
| > | > right? Well, in that case a long in C is 32 bit (or could you name
me a
| > | > Windows C compiler that has 64 bit longs?) and the PInvoke layer
will
| > pass
| > | > an UInt64 as a "long long" (64 bit integer) which is not what is
| > expected.
| > | >
| > | >
| > | >
| > | > Willy.
| > | >
| > | >
| > | > | > | > | That depends.
| > | > |
| > | > | The size of an int is defined as the word size of the CPU. A long
is
| > | > | only defined to be at least the size of an int.
| > | > |
| > | > | Willy Denoyette [MVP] wrote:
| > | > | > Not at all, UInt64 is 64 bit, a long in C is 32 bit!
| > | > | >
| > | > | > Willy.
| > | > | >
| > | > | > | > | > | > | Hello,
| > | > | > |
| > | > | > | Try to use System.Byte[100] for unsigned char[100] and
| > System.UInt64
| > | > for
| > | > | > | unsigned long.
| > | > | > |
| > | > | > | Mihaly
| > | > | > |
| > | > | > | "Arda Zuber" wrote:
| > | > | > |
| > | > | > | > Hi everyone.
| > | > | > | > I have a .dll that i want to call from a c# source. But i
can't
| > | > figure
| > | > | > out a
| > | > | > | > way...
| > | > | > | > Here are the .dll's input variables and the definition.
| > | > | > | >
| > | > | > | > extern "C" void _stdcall MyFunc(unsigned char var1[100], int
| > length,
| > | > | > | > unsigned long *arrFinal)
| > | > | > | >
| > | > | > | > And I want to call it by [DllImport(path,EntryPoint=MyFunc)]
| > | > | > | > public static extern void MyFunc(here comes the variables)
| > | > | > | >
| > | > | > | > but i cannot figure out the replacements for unsigned char
and
| > | > unsigned
| > | > | > long
| > | > | > | > * types in c#.
| > | > | > | >
| > | > | > | > Can you help me? Thanks.
| > | > | >
| > | > | >
| > | >
| > | >
| >
| >
 

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