Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

function import

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas Fisher - 08 Sep 2004 20:34 GMT
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
BMermuys - 09 Sep 2004 01:33 GMT
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?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.