473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Managed code in a regular dll

I have a solution written entirely in visual basic.net and I am trying to
interface it with an "old school" program. This old school program expects a
"c style dll" and a function exported from that dll for it to call.
I attempted to do this by creating a managed c++ project, exporting a
function with extern "C" _dllspec(export). In the function, I call to my
vb.net class library.

The problem is that I am not a c++ guru (and never was). I understand the
syntax but it's not my thing and something about this project just does not
seem right. Is there a right way of doing this: "bridging an c style dll to
managed code written in vb.net"?

Many thanks in advance.
Nov 17 '05 #1
1 1038
Hi,
Im a VC++ guru and like to do some things in VB(A) sometimes too, so maybe I
might be of your help..

"vbMental" wrote:
I have a solution written entirely in visual basic.net and I am trying to
interface it with an "old school" program. This old school program expects a
"c style dll" and a function exported from that dll for it to call.
I attempted to do this by creating a managed c++ project, exporting a
function with extern "C" _dllspec(export). In the function, I call to my
vb.net class library.

The problem is that I am not a c++ guru (and never was). I understand the
syntax but it's not my thing and something about this project just does not
seem right. Is there a right way of doing this: "bridging an c style dll to
managed code written in vb.net"?


If the dll is written and compile in c++ then you need to have following
things :
DLL must be a "regular DLL" (using MFC either as static/shared libraries).
Export things using (example) :
in header-/ and source-file (h/cpp-files) :
extern "C" void WINAPI MySub(LPCTSTR param1, LPTSTR param2);
extern "C" BOOL WINAPI MyFunc(long param1, int param2);

in definition-file (def) :
MySub
MyFunc

In (either) VB or VBA :
' for a procedure...
Declare Sub MySub Lib "MyC_Dll" ( _
ByVal param1 As String, _
ByVal param2 As String, _
)
' for a function...
Declare Function MyFunc Lib "MyC_DLL" ( _
ByVal param1 As Long, _
ByVal param2 As Integer, _
) As Long ' a C++ BOOL is either 0 for FALSE or anything else for TRUE

when transporting a VB-string to C++ it has to be converted to MultiByte
characters (while VB uses the BString type/ aka StringVariant).

when transporting vb-strings to a c-dll (either as LPCTSTR or LPTSTR), do
the following :
dim myString as String
myString = String$(260, vbNullChar) ' c's MAXPATH=260chars
' if you'd want to give a c-NULL pointer to a c-function then call it as
follow:
call MySub(myString, vbNullString) ' in C/C++ it's like "MySub(s, NULL);"
you cannot pass a vb-string to get it filled by a c-dll, first fill a
stringvariable with vbNullChar characters (see above, as I did with
'myString') then pass it as a parameter to the c-function.
When the c-function has finished, do the following to get back a vb-string :

Dim X As Integer, newString as String
X = InStr(myString, vbNullChar)
If X > 0 Then
newString = Left$(myString, X - 1)
Else
newString = myString
End If

Hope I helped you a bit
....
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yoni Rabinovitch | last post by:
Hi, We have a large code base of "regular" C++ code (not MFC, COM or ATL). With VC6.0, we used to compile the "regular" C++ code as static libraries (.lib files), and then we would link the...
5
by: Chris Kiechel | last post by:
I am writing a .NET Windows application and it needs to perform DDE calls to a legacy system. I created a C++ unmanaged class that performs the actual DDE connection and communication. However,...
4
by: 0to60 | last post by:
I'm trying to create a .dll with VS.NET 2003 Architect that contains a math computational component. I need the guts of the thing to be in native code, as performance is key for that part. But, I...
3
by: Dave | last post by:
I'm at a point where I would really like to focus in on learning .NET but am having a hard time deciding which language to use. I learned to program in C++ but have spent quite a bit of time using...
3
by: The unProfessional | last post by:
I'm having difficulty figuring out a simple way to dump unmanaged structure and/or class data to binary files. In standard C++: -------------------------- typedef struct tagS1 { DWORD dwBlah;...
9
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
2
by: Pohihihi | last post by:
Hello NG, Is managed C++ of same power as regular C++. Also what is the respect level for managed C++ in compare to regular C++. Cheers, Po
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
2
by: Joe | last post by:
I am trying to get a good understanding of these concepts and how they apply to code and classes (possibly different). As well as MSIL and Native Code (x86 assembly). To facilitate discussion...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.