accessing C++ functions

G

Guest

I am trying to call some API functions written in C++ from my VB .NET 2003
application and am running into some difficulty in passing structures to a
function. It is probably due to the types I'm declaring for the structure
members not lining up properly, but I don't know how to declare them
correctly. The C++ types being used are ULONG, LONG and LPWSTR. Does anyone
know what the VB type should be for each of these?

Thanks,
Stephanie
 
M

Michael M.

LPWSTR should be a pointer

it means LONG POINTER toWIDE / double byte STRING. as far as I can remember
anyway.

If it has a "C" LPCWSTR in there it means a "constant string".

I think there is a functon in the interop namespace to create a pointer for
a VB string.

Mike.
 
G

Guest

When I tried declaring the "ULONG" members as UInteger, it didn't recognize
that type. My options were UInt16, UInt32 and UInt64. Which of these should
I choose, or is it something altogether different?

S

None of the structure members are
 
G

Guest

Sorry, I assumed VB 2005.
For older versions of VB.NET, use UInt32.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
G

Guest

Ok. the UInt32 is in there. Now I am having a problem because one of the
UInt32 members is a mask that is set using some constants that I had defined
like:

Public Const PWEF_DATASOURCE = 2

and worked when the mask member was a ULONG. Now that the member is a UInt32
and I try to set its value using:

lpPweParam.ulMask = PWEF_DATASOURCE Or PWEF_USERNAME Or PWEF_PASSWORD Or
PWEF_FOLDER Or PWEF_ITEMTYPE

I get the compiler error:
Value of type 'Integer' cannot be converted to 'System.UInt32'.

I tried substituting:
lpPweParam.ulMask =System.Convert.ToUInt32(2) or System.Convert.ToUInt32(4)
or .....

but then got the compiler error:
Operator 'Or' is not defined for types 'System.UInt32' and 'System.UInt32'.

How do I define a mask for use with the UInt32 datatype?


Thanks,
S
 
G

Guest

I've fixed that problem by creating an integer variable, assigning it the
mask and then assigning the structure variable using the
System.Convert.ToUInt32(in mask). However, the function is still not working,
so it looks like I'm down to working with the LPWSTR members.

Michael M. wrote about using a function in the Interop namespace to create
pointers to String. I couldn't find info on those functions. Can someone
point me in the right direction there?
 
M

Michael M.

This is done in VB 2005 free edition, I am not sure if shuch features exist
in your version



If you are just sending a string to the functon and don't need to read it
back then the following is fine

Imports System.Runtime.InteropServices

Dim sLocalString As String = "blah blah"

Dim pString As IntPtr = 0

pString = Marshal.StringToHGlobalAuto(sLocalString)

Call SomeApiFunction(pString)



if you need to read it back then would need to allocate a buffer and then
use the StringFromPtr functions.
 

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