Interop: failure returning a value from unmanaged dll

C

ConfNoob

part 1: there is a NON - COM unmanaged dll in VC++ 6.0
BOOL function(char * name, int nOption)

part 2: there is a C# web service that has to consume the above DLL
used DLLImport to call the Non-COM Dll.
[DllImport("Dllname.dll", EntryPoint = "function",

used "Debug Unmanaged Code" to step into DLL.

the DLL gets the right values, processes the steps properly.

however when the value is returned to the C# Webservice
no matter if the return from the unmanaged DLL is TRUE (1) or FALSE
(0), the bool in the C# webservice is always 0/false

the DLL DOES return BOOL which is a 4 byte int , hence im led to
believe that it should be marshalled properly into a C# 'bool'

as alternatives have done the following.

- tried casting the BOOL to an int using Convert.ToInt32()

trying to convert the ENTIRE unmanaged DLL into a COM application is
not something i am able to do ....

suggestions ??

TIA
 
N

Nicholas Paldino [.NET/C# MVP]

ConfNoob,

You haven't shown the declaration of the function in C#. Can you
provide that?

Also, are you sure that the calling convention is correct?
 
C

ConfNoob

public static extern bool function(string name,int nOption);

is how its declared in C#

"CallingConvention = CallingConvention.StdCall"

is what i've specified in the DllImport()

thanks again
 
W

Willy Denoyette [MVP]

| public static extern bool function(string name,int nOption);
|
| is how its declared in C#
|
| "CallingConvention = CallingConvention.StdCall"
|
| is what i've specified in the DllImport()
|
| thanks again
|

Try this: declare the function to return an int and check for the value 0 or
!= 0.

public static extern int function(string name,int nOption);

....

if(function(string name,int nOption) != 0)
// function returned non 0 (TRUE) value
else
// function returned 0 (FALSE)

If this works, the bool version should work also.

Willy.
 
W

Willy Denoyette [MVP]

| public static extern bool function(string name,int nOption);
|
| is how its declared in C#
|
| "CallingConvention = CallingConvention.StdCall"
|
| is what i've specified in the DllImport()
|
| thanks again
|

Oh, and who guarantees that the function uses the stdcall convention?

Willy.
 
C

ConfNoob

hi Willy,

do you suggest that i make the change in the VC++ DLL to return an int
?

thanks again.
 
W

Willy Denoyette [MVP]

No, I just need to be sure the return type is a BOOL (which is typedef'd as
an int), so I like to see the value 0 for FALSE and !=0 for TRUE returned.
If this is true, the signature bool function(...) is correct and bool should
be true or false depending on the value of the int, however, if the C return
type is a bool (a byte), then the value of bool will always be fase as the
interop layer expects an int returned from C.

Willy.

| hi Willy,
|
| do you suggest that i make the change in the VC++ DLL to return an int
| ?
|
| thanks again.
|
 

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