Solved

calling vc++ dll from vb

Posted on 2002-03-03
5
261 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

759 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

17 Experts available now in Live!

Get 1:1 Help Now