.NETGURU
function import
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.

Post a new message to this list...

Thomas Fisher
Hello,

This is a rephrasing of a previous question that I didn't get an answer to,
maybe putting it differently will help.

In Baf.dll there is a function prototype:

BOOL CDECL LaunchRunner (LPCSTR szUserDisplay, BOOL* pbEnabled);

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
posted code in the previous post (on 8/6).

Thanks a lot for any help with this...

- Tom

Reply to this message...
 
    
BMermuys
Hi,

"Thomas Fisher" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

<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

[Original message clipped]

Reply to this message...
 
    
Thomas Fisher
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!

Reply to this message...
 
    
Robert Jordan
Hi Thomas,

[Original message clipped]

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
Reply to this message...
 
    
Thomas Fisher
Thanks for the help.

[Original message clipped]

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?

Reply to this message...
 
    
Robert Jordan
Hi Thomas,

[Original message clipped]

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
Reply to this message...
 
    
Thomas Fisher
[Original message clipped]

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?

Reply to this message...
 
    
Thomas Fisher
> <DllImport("...", CallingConvention:=CallingConvention.Cdecl,
CharSet:=CharSet.Ansi) > _
> Public Function LaunchRunner( ByVal szUserDisplay As String, ByRef
pbEnabled As Boolean) As Boolean
> End Function

[Original message clipped]

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...

Reply to this message...
 
    
BMermuys
Hi,

"Thomas Fisher" <thesequoyan [[[a]]] hotmail> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Do you have some code in any language that uses the function which works ?

[Original message clipped]

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

[Original message clipped]

Reply to this message...
 
    
Thomas Fisher
[Original message clipped]

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?

[Original message clipped]

Because of what I read in this thread:
http://www.error-bank.com/microsoft.public.dotnet.languages.vb.1/205640_Thread.aspx

Reply to this message...
 
 
System.Boolean
System.IntPtr
System.Runtime.InteropServices.CallingConvention
System.Runtime.InteropServices.CharSet
System.Runtime.InteropServices.GCHandle
System.Runtime.InteropServices.GCHandleType
System.Runtime.InteropServices.Marshal
System.Runtime.InteropServices.UnmanagedType
System.String
System.Threading.Thread




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification