Connecting Tech Pros Worldwide Help | Site Map

Interop: failure returning a value from unmanaged dll

ConfNoob
Guest
 
Posts: n/a
#1: May 3 '06
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

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: May 3 '06

re: Interop: failure returning a value from unmanaged dll


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?


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"ConfNoob" <Ronodev.Sen@gmail.com> wrote in message
news:1146664824.723801.317390@i40g2000cwc.googlegr oups.com...[color=blue]
> 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
>[/color]


ConfNoob
Guest
 
Posts: n/a
#3: May 4 '06

re: Interop: failure returning a value from unmanaged dll


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

Willy Denoyette [MVP]
Guest
 
Posts: n/a
#4: May 4 '06

re: Interop: failure returning a value from unmanaged dll



"ConfNoob" <Ronodev.Sen@gmail.com> wrote in message
news:1146710450.200439.116440@g10g2000cwb.googlegr oups.com...
| 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.


Willy Denoyette [MVP]
Guest
 
Posts: n/a
#5: May 4 '06

re: Interop: failure returning a value from unmanaged dll



"ConfNoob" <Ronodev.Sen@gmail.com> wrote in message
news:1146710450.200439.116440@g10g2000cwb.googlegr oups.com...
| 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.



ConfNoob
Guest
 
Posts: n/a
#6: May 5 '06

re: Interop: failure returning a value from unmanaged dll


thanks Willy for the replies.

will try those.

ConfNoob
Guest
 
Posts: n/a
#7: May 8 '06

re: Interop: failure returning a value from unmanaged dll


hi Willy,

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

thanks again.

Willy Denoyette [MVP]
Guest
 
Posts: n/a
#8: May 8 '06

re: Interop: failure returning a value from unmanaged dll


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.

"ConfNoob" <Ronodev.Sen@gmail.com> wrote in message
news:1147053514.533784.117140@i39g2000cwa.googlegr oups.com...
| hi Willy,
|
| do you suggest that i make the change in the VC++ DLL to return an int
| ?
|
| thanks again.
|


Closed Thread