Archived

This topic is now archived and is closed to further replies.

__JoshMan__

Visual C++ lib to VB

Recommended Posts

Hey Guys, I''m programming a 3D graphics engine in MSVC6. My plan is to compile it to a static library (".lib") file when it''s finished, and import it into any future apps in which I may want 3D graphics. Although this plan will work for any apps that I''m going to make, my little brother (a Visual Basic programmer) wants to use the engine in his apps. How would I go about converting a Visual C++ static library file into a visual basic True Type Library file? I know that this can be done by programming an ActiveX and importing it into a VB app, but I''d prefer simply converting my C library into a True Type Library. Thanx in advance, JoshMan

Share this post


Link to post
Share on other sites
quote:
Original post by __JoshMan__
...Visual C++ static library file into a visual basic True Type Library file?

Um, doesn''t True Type refer to fonts?

I don''t know about static libraries (due to considerations like name mangling), but you can write a dynamic link library and use it with any (serious) Windows development language - C, C++, VB...

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Well, let''s just repurpose my answer :

You can write a dynamic link library and use it with any (serious) Windows development language - C, C++, VB... I''ve never heard anyone mention doing it for static libraries, probably because of the increase in executable size.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
I don''t think it is possible to import a VC++ static library into VB. A library is essentially a precompiled file from which native code can be obtained in generating a final executable. VB doesn''t have this type of capability like C++. I think the DLL answer is the way to go.

-Kirk

Share this post


Link to post
Share on other sites
In VB:

Declare Function MyFunc Lib "myengine.dll" (ByVal Arg1, Arg2, Arg3) As Long

Replace names as neccessary. Pain in the ass, but its the only way.

NOTE: the DLL must be properly registered, in the Windows\System directory, or int he App directory!

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
properly registered = not good.
you dont need to register anything. just keep the engine.dll file in the apps directory since ppl dont want crud in their system directory which is for system components (not applications componets unless by microsoft which makes them system components anyway).

and do what promit says. also to add, creating a dynamic link library means you have to have non mangled names (face its using a static lib wont be helpful) use the NOMANGLE keyword before all exported functions. thsi will ensure vc++ will keep normal names.
(or extern "C" can be used which i think is what NOMANGLE actually is defined as) and yes you need the quotes around the C.

Share this post


Link to post
Share on other sites
you could always write one of those "type libraries" for your DLL, and then include it in the "Project->References" thing in VB. that way, you won't have to declare all the functions in the VB code (as Promit told you to do).

i don't think this makes a difference, except that it requires less effort from your little brother, and more from you.

--- krez (krezisback@aol.com)

Edited by - krez on December 13, 2001 2:17:30 PM

Share this post


Link to post
Share on other sites
quote:
Original post by ANSI2000
1- Visual Bassic is for secretaries.

No, it''s for managers who would rather get their apps running quickly than ponder why the dereference on that iterator is yielding a garbage value.

quote:
2- Have your brother learn C++ and Visual Studio.

How do you learn "Visual Studio."

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!"

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites