Link to home
Start Free TrialLog in
Avatar of kkamm
kkammFlag for United States of America

asked on

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

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
Avatar of lakshman_ce
lakshman_ce

>>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

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.

Avatar of kkamm

ASKER

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?
SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kkamm

ASKER

>> 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.
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kkamm

ASKER

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.
Avatar of kkamm

ASKER

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.