Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

calling vc++ dll from vb

hello,
     I want to create a DLL in VC++ and call methods from vb program.can anybody help with some sample code
0
raju_r
Asked:
raju_r
1 Solution
 
jlsjlsCommented:
Read following interesting link (samples + explanation) http://www.vbexplorer.com/show.asp?id=196

Hope this helps.
0
 
smeggheadCommented:
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
 
christophmCommented:
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
 
fatih_teryakiCommented:
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
 
smeggheadCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now