1

This question is an extension of a previous question: Return an object in VBA

Now, I'd like to know how to declare and initialize the object in VBA. It seems like I'd do it like so:

Declare Function ConnectMe Lib "C:\Windows\System32\cm.dll" (ByVal Arg1 As String) As ConnectMe
Declare Function login Lib "C:\Windows\System32\cm.dll" (ByVal Arg1 As String, ByVal Arg2 As String) As Boolean

Then, below this line, I could use this code:

dim cm as new ConnectMe

cm.ConnectMe("216.239.51.99")

cm.login("username","password")

However, when I do this, it gives me a "User-defined type not defined" error. How can I declare this C++ class appropriately so that I can create and use an instance in VBA?

Thanks.

2 Answers 2

2

This code is never going to work as it is. If you want to create a class in C++ under Windows and use it in any other programming language that is not C++, (VB for example) the "normal" approach is to create a COM class or an ActiveX control if you need to draw graphics.

You may also find this answer in SO helpful.

Sign up to request clarification or add additional context in comments.

Comments

0

i here again :D

To use this code with your function use:

Dim cm as Object

set cm = ConnectMe("parameter")

if cm.login("username","password") then 
     msgbox "Connect!",vbinformation
else
     msgbox "not connect!",vbinformation
end if

9 Comments

But how do I declare the function to be available? I'm thinking @yms is correct, that I'm going to need to approach this from an entirely different angle.
Sam look this msdn.microsoft.com/en-us/library/bb687915.aspx in this page has same answer.
I found what I believe you're referring to: "[Public | Private] Declare Function name Lib "libname" [Alias "aliasname"] [([arglist])] [As type]" However, when I try to specify the 'ConnectMe' class with "Declare Function ConnectMe Lib "C:\Windows\System32\cm.dll" (ByVal Arg1 As String) As ConnectMe" it complains about the user-defined type. Any suggestions?
You registered your DLL with REGSVR32 C:\windows\System32\cm.dll ?
That page does not mention references to classes or objects. As I mentioned in my answer, this problem is not only for C++ to VBA. An instace of a (non-COM) C++ class cannot be used directly in any other programming language.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.