Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

calling vc++ dll from vb

Posted on 2002-03-03
5
Medium Priority
?
277 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

722 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