Solved

I have a C++ (and associated header) source file containing routines that I would like to use in VB.NET app. Possible? How?

Posted on 2006-07-06
9
199 Views
Last Modified: 2010-04-24
What I've tried:

1)Compile as DLL with __declspec( dllexport ) on desired routines. Name mangling occurs. Unuseable.
2)Compile as DLL with extern "C" __declspec( dllexport ) on desired routines to respond to "name mangling". Errors during build. Unuseable.
3)Compile as DLL and try to add as reference in VB.Net project. Error results. Unuseable.

I am NOT looking for calls into unmanaged code routines from managed code. Surely calling .NET C++ routines from a .NET VB app must be more straightforward and doesn't need "wrapper" layers. I was under the impression that .NET managed code from any of the .NET languages was fairly interchangeable with regards to function calling,etc. (Or is this yet another Microsoft "true but with caveats" adventure?)

What is the best way to accomplish this without doing a port of the C++ code? If I have to do a port then I might as well just do the whole thing in C++ which is what I wanted to avoid in the first place.

BTW-I am using Visual Studio 2005
0
Comment
Question by:kkamm
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
Comment Utility
>>3)Compile as DLL and try to add as reference in VB.Net project. Error results. Unuseable.

what is the error you are getting.

You would be able to access the methods in dll after adding the reference. Try to see the functions in dll after adding the reference.


You can also take a look at this sample
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=06613cc5-2134-404c-a4e2-ca46ef086c12

0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
You have two options:

1) PInvoke. In this case you need to write Dll which exports API.

>> Compile as DLL with extern "C" __declspec( dllexport ) on desired routines to respond to "name mangling". Errors during build. Unuseable.

This is the way. What errors exactly do you have?

2) Using C++/CLI wrapper, which exposes .NET interface and internally calls unmanaged C++ code.

0
 
LVL 1

Author Comment

by:kkamm
Comment Utility
Partial success (name mangling seems to have cleared up) by adding .DEF file into project and adding "__stdcall" to routines that I want to export but I am still getting errors when trying to add the DLL reference to my VB.Net project. Error indicates that it is not a valid assembly.

Alex:

>>1) PInvoke. In this case you need to write Dll which exports API.

Isn't this for calling unmanaged COM objects or does it also have to be used on .NET objects? I was thinking that .NET objects could be easily referenced across .NET languages and be called in a fairly straightforward fashion.

As an aside I should mention that my DLL entry point is in one main CPP file while the routines I want to reference are in another CPP file. The example 'Lakshman ce' provided above worked fine but it had the exported routines in one CPP file. What do I need to do to reconcile this?
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 250 total points
Comment Utility
Adding reference is possible only for .NET and COM libraries. For unmanaged libraries which export functions you need to use PInvoke which works exactly like API calls in VB6.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconCallingWindowsAPIs.asp

>> I was thinking that .NET objects could be easily referenced across .NET languages and be called in a fairly straightforward fashion.
Yes, but unmanaged C++ library doesn't belong to .NET. The only .NET language which allows to call unmanaged code directly is C++/CLI.

>> As an aside I should mention that my DLL entry point is in one main CPP file while the routines I want to reference are in another CPP file.
Exported functions can be placed in any cpp file, it doesn't matter.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:kkamm
Comment Utility
>> Adding reference is possible only for .NET and COM libraries.

 The DLL project is being assembled and compiled within Visual Studio 2005. In order for the DLL to be useable as a .NET library is it enough to just compile it within VS2005 or do I need to do some special preparation to accomplish this?

>>For unmanaged libraries which export functions you need to use PInvoke

I thought this methodology was used only for legacy DLL's,etc. but wasn't neccessary in a .NET scope. If I write some VB.NET code with exported functions and compile it into a DLL is it possible to access those functions from a C++ .NET project without using  PInvoke?

Sorry if this seems rudimentary. I am working with an SDK that supplied C++ source code but no precompiled libraries and my C++ ability is remedial. I would like to do my part of the coding in VB.Net but I need to make use of these routines.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Visual Studio 2005 allows to build both managed and unmanaged projects. Project built in VB .NET, C# and C++/CLI are managed. Managed library written in one of managed langauges can be used by client written in any .NET languages.
Unmanaged C++ project can be built in VS 2005, but it still remains unmanaged. Such project cannot be added as reference, and can be called by .NET client using PInvoke.

Again, you have two options: PInvoke or C++/CLI wrapper.
0
 
LVL 15

Accepted Solution

by:
lakshman_ce earned 250 total points
Comment Utility
Kkamm

Did you look at the sample in the link in my post.?

sample code from the sample
1. Add __stdcall to method definition

BOOL __stdcall ShutdownSystem()
{

}
2. Update .def file to resolve name mangling
LIBRARY      Powercut

EXPORTS  
    ShutdownSystem      @1
3. Copy your C++ DLL to System directory
3. In VB.NET code declare method
Private Declare Function Shutdown Lib "Powercut.dll" Alias "ShutdownSystem" () As Boolean

4. Call Shutdown() to execute method from dll


0
 
LVL 1

Author Comment

by:kkamm
Comment Utility
Lakshman,

I did try it and it worked fine within the context of the sample but when I applied it to my project I got an exception:

<<An error occurred creating the form. See Exception.InnerException for details.  The error is: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.>>

I think I may be casting my parameters to the wrong type when I make the call into the DLL. I'll see if I can get that resolved first.
0
 
LVL 1

Author Comment

by:kkamm
Comment Utility
I resolved the error by recasting the calling parameters but the call seems to be returning unuseable values. That is a project level issue and not a conceptual one (i.e. my problem :) ) so I will consider the initial question answered.

I used bits from each of your posts so I split the points evenly.

Thanks guys.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now