C++ DLL Usage (p/invoke?)

D

Dave

Hello, I have little experience with VB or .Net in general but have a
DLL control written in C++ using MFC -- it is a graphical routine that
takes place primarily in OnPaint. Is there a way to insert this into
VB? If so are there examples I could download?

My primary concerns are 1) where to put this in the VB project, and 2)
Converting C++ parameters to .Net variable types.

I'm looking at the technique of using platform invoke described here:
http://www.codeproject.com/dotnet/PInvoke.asp?print=true


Is there a way to import the following C++ 'create' command into VB?

BOOL CreateCtrl(CWnd* pParentWnd, int top, int left, int bottom, int
right, UINT nID, DWORD dwStyle = WS_VISIBLE);


I'm trying the following in Form1.vb but can seem to get it to work:

<DllImport("MyMFCDLL.dll")> Public Shared Function CreateCtrl(ByVal
prnt As Object, ByVal top As Int32, ByVal left As Int32, ByVal bottom
As Int32, ByVal right As Int32, ByVal id As UInt32) As Boolean
End Function


Thanks.
 
L

Lucian Wischik

Dave said:
Is there a way to import the following C++ 'create' command into VB?
BOOL CreateCtrl(CWnd* pParentWnd, int top, int left, int bottom, int
right, UINT nID, DWORD dwStyle = WS_VISIBLE);

No. Its first argument is CWnd, an MFC wrapper around a win32 window
(also known as HWND or Handle). But VB will only give you
..net/winforms wrappers around Handles.

Did you write the DLL, or someone else? If you have the source code,
maybe you can recode it a little to take a HWND as its first argument.
I know enough MFC to believe that this is possible, but have no solid
ideas. Once you've done this,

Public Shared Function CreateCtrl(ByVal prnt As IntPtr, ...)

NB. You seemed to be missing the final dwStyle argument? That's
compulsory in your declaration of CreateCtrl. Also, you had it return
a Boolean. I don't think that's right. I think you have to use
Interop.Marshal to convert an mfc BOOL into a VB Boolean. I don't know
the VB syntax. Here's an example of the c# syntax for marshalling a
bool:
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool OpenClipboard(IntPtr hwnd);



If you don't have the source code then the solution is more
horrendous. You'd write your own MFC container, one which has one of
these controls inside it, but which accepts an HWND as its parent.
 

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