Solved

calling vc++ dll from vb

Posted on 2002-03-03
5
262 Views
Last Modified: 2010-04-01
hello,
     I want to create a DLL in VC++ and call methods from vb program.can anybody help with some sample code
0
Comment
Question by:raju_r
5 Comments
 
LVL 3

Expert Comment

by:jlsjls
ID: 6838583
Read following interesting link (samples + explanation) http://www.vbexplorer.com/show.asp?id=196

Hope this helps.
0
 
LVL 10

Expert Comment

by:smegghead
ID: 6839794
1. Create a new project using the ATL COM AppWizard (call it MyApp) - select 'Uses MFC' from the first page of the wizard, accept all other defaults.
2. From the menus, click Insert...New Class
3. Give the (ATL) class a name (call it MyClass) and click OK
4. Within the 'WorkSpace' window, select the classview tab
5. Right click on the item that looks like a magnifying glass (called iMyClass) and click 'add property'.
6. For the property type, use 'long'
7. For the function name use 'GetLen'
8. For the parameters, use 'BSTR InString'
9. Uncheck the 'Put Function' and Click OK
10. Expand the class item (MyClass - with the three coloured boxes next to it), then expand the magnifying glass icon (iMyClass), to show all properties / methods and double click the one you've just added
11. The code window will show the insertion point for your code

overwrite the

// TODO: Add your implementation code here

with

CString TempString(InString);
*pVal=TempString.GetLength();


12. Build your application (F7)
13. Launch VB, and add the reference MyApp 1.0 Type Library
14. Add the following code to the form_load event

    Dim x As New MyClass
    MsgBox x.GetLength("hello")

15. Run the project and "5" should pop up on the screen

it's that easy !!! :-)
0
 
LVL 2

Expert Comment

by:christophm
ID: 6844537
Hi raju_r,

I used to write DLLs for my VB buddies until I discovered how easy it was to write automation components. Automation Components (to supply services to VB programs) have these advantages over DLLs:

1. Are used in the same manner ('get/set methods', properties') as are the 'standard' automation components with which VB users are familiar.  (Microsoft Office products Word/XL/PP/etc. expose their 'facilities' through automation)

2. Much easier to write and maintain then DLLs; for example no struggling with mangled names or DEF files - no messy parameter lists.

3. Called/Used in exactly the same manner whether from VB, VBA, or VBSCript (using automation components in MFC/C++ is the easiest way I have found of doing C stuff from VBScripting).

I have written a comprehensive 'how-to' cookbook on how to write an automation component.  You will find it at
    www.teleport.com/~millercg/automation.txt
Give automation components a try - Your VB guys will be happy that they are so easy (and familiar) for your VB programmers.

chris miller (christophm)
0
 

Accepted Solution

by:
fatih_teryaki earned 100 total points
ID: 6856035
Simply create a dll with new project wizard.
Things to do;
1. If you are using MFC make the first line of the exported functions following;

AFX_MANAGE_STATE(AfxGetStaticModuleState());

2. Export your functions as __stdcall like this;

extern "C" __declspec(dllexport)
int __stdcall func(char *s)
{
 .
 .
 .
}

In quick view this function will be seen as _func@4

In VB
Declare Function func Lib "mylib" _
Alias "_func@4" (ByRef s As String) As Long

0
 
LVL 10

Expert Comment

by:smegghead
ID: 6856086
fatih,

There are 3 other proposed solutions, all of which will point raju in the right direction.

It is up to raju who he credits for the best advice given, it is not fair on the other people who leave comments to propose answers as this locks the question and prevents it from being displayed in the main list of questions.

If you post a comment and raju finds your comment the most useful, he can accept your comment as an answer (or anybody else's)

Smg.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now