Solved

calling vc++ dll from vb

Posted on 2002-03-03
5
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

739 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