Visual C++ lib to VB

Started by
14 comments, last by __JoshMan__ 22 years, 4 months ago
As far as I know, there are two ways that you can call into C/C++ code from vb:
dynamic link Dll - through declare statements as stated above - you just need to know the path of the library
COM - I believe you''ll have a few retrictions in the types you can use, but you can typically reference COM Dlls from VB6 - check OLE data types for this - this also involves adding a type library in some way, either embedded in the dll or as a separate file that you''ll reference. The cool thing is, structures can be exposed through this, unlike through declares where you''ll have to redefine them in vb6


Hope that helps

You know, I never wanted to be a programmer...

Alexandre Moura
Advertisement
Actually, if you write a program which will set up a message hook, you can compile VB and VC++ code together. You look for when VB runs the program "link.exe" and modify the arguments. If you go to the command line and type "link /?" there are details on how it works. From memory, you can also increase the pathetically small stack space from there too.

Trying is the first step towards failure.
Trying is the first step towards failure.
I still say VB is for secretaries

How do you learn Visual Studio?? Well every good programmer knows that you also have to know the ins and out of your development enviroment. When I say Visual Studio as in Visual C++ and Tools
Okay, first of all... its not true type library, its just Type Library (*.tlb) if you even meant that. Also, I agree with the common opinion that a (*.dll) would be better. Reason #1: easier to update and get working with multiple development tools. Reason #2: no-one actually seems to know (correct me if I''m wrong) how to make a .tlb. Reason #3: try to get a .tlb working with VC6++

Here''s a sample resource script (.rc) You should have one created for your project because it makes developing it a bit easier.

//Microsoft Developer Studio generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// Finnish resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FIN)
#ifdef _WIN32
LANGUAGE LANG_FINNISH, SUBLANG_DEFAULT
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END

3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END

#endif // APSTUDIO_INVOKED


#ifndef _MAC
/////////////////////////////////////////////////////////////////////////////
//
// Version
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x7L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "Comments", "Test\0"
VALUE "CompanyName", "Me, who else ?\0"
VALUE "FileDescription", "asdf\0"
VALUE "FileVersion", "1, 0, 0, 1\0"
VALUE "InternalName", "asdf\0"
VALUE "LegalCopyright", "Copyright © 2001\0"
VALUE "LegalTrademarks", "\0"
VALUE "OriginalFilename", "asdf.lib\0"
VALUE "PrivateBuild", "\0"
VALUE "ProductName", "Me\0"
VALUE "ProductVersion", "1, 0, 0, 1\0"
VALUE "SpecialBuild", "\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END

#endif // !_MAC

#endif // Finnish resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

Now, if we observe the VS_VERSION_INFO block and its members
FILEOS 0x40004L
FILETYPE 0x7L
FILESUBTYPE 0x0L
we notice that we''re compiling for VOS_NT_WINDOWS32 which means it will be compatible with windows platforms. File type will be VFT_STATIC_LIB that means you are compiling a static library. File subtype is VFT2_UNKNOWN and it cannot be changed. You cannot compile a static library so that it could be used in VB (to the best of my knowledge). If you want a (*.tlb) it will be less versatile and you have to change the VFT_STATIC_LIB to VFT_UNKNOWN

FILEOS 0x40004L
FILETYPE 0x0L
FILESUBTYPE 0x0L

And do some weird tricks to make it into a TLB. That was my best guess about it.

I still recommend you make a DLL.
As for your little brother... either make him use C++ or then tell him to download DX8 SDK for newb... VB that is and make an engine of his own.

- A.J. -
"Where''s the KABOOM!? There was supposed to be an earth-shattering KABOOM!"
- A.J. -"Where is the KABOOM!? There was supposed to be an earth-shattering KABOOM!"
quote:
Reason #2: no-one actually seems to know (correct me if I'm wrong) how to make a .tlb. Reason #3: try to get a .tlb working with VC6++

I know how.... and it's pretty easy to use a tlb with VC.

First, you add a file to you project with .idl as the extention. Then you learn the Interface Defintion Language, and write idl code that describes the functions you're exporting. Then you compile the idl file, using the midl compiler. It will produce a header file for use with your dll code and a tlb file to include into the dll project as a resource. Making a COM dll isn't that much harder - and is actually easier if you use the ATL (built into VC6).

A crude tutorial on making a COM dll in C++ for use in VB using the ATL, can be found here.

Magmai Kai Holmlor

"Oh, like you've never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

P.S. The code to use a dll with a typelibrary in VC is at the end of the tutorial.

Edited by - Magmai Kai Holmlor on December 14, 2001 8:07:36 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:Original post by Oluseyi
How do you learn "Visual Studio."


You''d be surprised. There are people in my immediate vicinity right now that don''t know that Visual Studio has record/reply macro capabilities. Just the other day I found out there''s a builtin and unassigned by default command to make a word all uppercase (and one for lowercase and one for just capitalize) - so I bound that to my ctrl-shift-< key and quickly wrote a macro to change a list of filenames to enums.

And then there''s the whole plugin system for it...

My point is - while there''s not *a lot* to learn about Visual Studio, and I''m sure the original poster didn''t mean what I''m talking about, but it''s a legitimate job skill to have.

-scott

This topic is closed to new replies.

Advertisement