Hi,
> Hello,
>
[quoted text clipped - 5 lines]
>
> BOOL CDECL LaunchRunner (LPCSTR szUserDisplay, BOOL* pbEnabled);
<DllImport("...", CallingConvention:=CallingConvention.Cdecl,
CharSet:=CharSet.Ansi) > _
Public Function LaunchRunner( _
ByVal szUserDisplay As String, _
ByRef pbEnabled As Boolean) As Boolean
End Function
If it doesn't work then maybe your c sign isn't right, the function doesnt
work properly or BOOL is different then the one in wtypes.h
HTH,
greetings
> How would this be defined in VB.Net or C# and how should I call it? I've
> tried everything I can think of and am getting null reference errors. I
[quoted text clipped - 3 lines]
>
> - Tom
Thomas Fisher - 09 Sep 2004 05:57 GMT
> <DllImport("...", CallingConvention:=CallingConvention.Cdecl,
CharSet:=CharSet.Ansi) > _
> Public Function LaunchRunner( ByVal szUserDisplay As String, ByRef
pbEnabled As Boolean) As Boolean
> End Function
> If it doesn't work then maybe your c sign isn't right, the function doesnt
> work properly or BOOL is different then the one in wtypes.h
Thanks for the reply! Ok, I've tried this and it still didn't work.
- by "c sign" are you referring to the declaration? If so, I'm sure
it's right.
- the function does work properly, i know because there is a control
panel that calls this function
- how would i know what type of bool to use? what others are there to
try?
Thanks for the help...
BMermuys - 09 Sep 2004 12:36 GMT
Hi,
>> <DllImport("...", CallingConvention:=CallingConvention.Cdecl,
> CharSet:=CharSet.Ansi) > _
[quoted text clipped - 11 lines]
> - the function does work properly, i know because there is a control
> panel that calls this function
Do you have some code in any language that uses the function which works ?
> - how would i know what type of bool to use? what others are there to
> try?
Well, the sign or declaration does it come from a header file ? If so,
check the header file if it defines or typedefs BOOL or if it includes any
other header which does so.
Something must be wrong, because I have a function with the same sign which
works without any problems.
Why do you allocate 255 chars for the string, does the function wants a
fixed size string ? The function doesnt alter the string does it ?
HTH,
greetings
> Thanks for the help...
Thomas Fisher - 09 Sep 2004 16:17 GMT
> Well, the sign or declaration does it come from a header file ? If so,
> check the header file if it defines or typedefs BOOL or if it includes any
> other header which does so.
Here's the definition from the header file:
BOOL CDECL LaunchRunner( LPCSTR szUserDisplay , BOOL* pbEnabled );
typedef BOOL (CDECL* fLaunchRunner)( LPCSTR szUserDisplay , BOOL*
pbEnabled );
> Something must be wrong, because I have a function with the same sign which
> works without any problems.
How do you call it? With a System.String and System.Boolean? Do you pin
them first?
> Why do you allocate 255 chars for the string, does the function wants a
> fixed size string ? The function doesnt alter the string does it ?
Because of what I read in this thread:
http://www.error-bank.com/microsoft.public.dotnet.languages.vb.1/205640_Thread.aspx
Thomas Fisher - 09 Sep 2004 07:01 GMT
Ok,
Here's an update of the code I'm using, still not working.
C++ Prototype from Baf.dll:
BOOL CDECL LaunchRunner (LPCSTR szUserDisplay, BOOL* pbEnabled);
C# Wrapper:
[DllImport("Baf.dll", CallingConvention=CallingConvention.Cdecl,
CharSet=CharSet.Ansi)]
public static extern bool LaunchRunner ( string szUserDisplay,
[MarshalAs(UnmanagedType.Bool)] ref bool pbEnabled ); //note: I've also
tried marshaling it is VariantBool and U1
VB.Net Call to C# Wrapper:
Dim buffer As String = "AB"
buffer.PadRight(255, vbNullChar)
Dim handle As GCHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned)
Dim sPtr As IntPtr = handle.AddrOfPinnedObject
Dim B As Boolean = True
Dim bPtr As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(B.GetType))
Marshal.StructureToPtr(B, bPtr, False)
If Not LaunchRunner (buffer, B) Then
Throw New Exception("LaunchRunner failed")
End If
Marshal.FreeCoTaskMem(bPtr)
handle.Free()
The function is returning false (no error) so something's not right but I
think I'm getting closer. It would be great if someone who knows more than
me could take a look at this and tell me what's wrong.
Thanks!
Robert Jordan - 09 Sep 2004 11:08 GMT
Hi Thomas,
> Ok,
>
[quoted text clipped - 9 lines]
> [MarshalAs(UnmanagedType.Bool)] ref bool pbEnabled ); //note: I've also
> tried marshaling it is VariantBool and U1
You don't need to manually marshal the string:
[DllImport("Baf.dll", CallingConvention=CallingConvention.Cdecl,
CharSet=CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool LaunchRunner(
[MarshalAs(UnmanagedType.LPStr)] string szUserDisplay,
[MarshalAs(UnmanagedType.Bool)] ref bool pbEnabled
);
bye
Rob
Thomas Fisher - 09 Sep 2004 16:21 GMT
Thanks for the help.
> You don't need to manually marshal the string:
>
[quoted text clipped - 5 lines]
> [MarshalAs(UnmanagedType.Bool)] ref bool pbEnabled
> );
I've changed the wrapper to match this and have tried calling it in several
ways and none work. An example call:
LaunchRunner("AB", True)
Any more ideas?
Robert Jordan - 09 Sep 2004 18:32 GMT
Hi Thomas,
> Thanks for the help.
>
[quoted text clipped - 14 lines]
>
> Any more ideas?
There are 2 kinds of unmanaged bools:
1 byte C++/C
4 bytes (WINAPI)
You may try whether the 1 byte version applies using
[MarshalAs(UnmanagedType.I1)] for both bools.
bye
ROb
Thomas Fisher - 09 Sep 2004 22:29 GMT
> There are 2 kinds of unmanaged bools:
>
[quoted text clipped - 3 lines]
> You may try whether the 1 byte version applies using
> [MarshalAs(UnmanagedType.I1)] for both bools.
Here's the definition from the header file:
BOOL CDECL LaunchRunner( LPCSTR szUserDisplay , BOOL* pbEnabled );
typedef BOOL (CDECL* fLaunchRunner)( LPCSTR szUserDisplay , BOOL*
pbEnabled );
I've tried it with I1 for both and still no go. I've been trying every
permutation i can think of...any other ideas?