Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
210 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
ID: 17052991
>>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
ID: 17053228
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
ID: 17053808
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
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 750 total points
ID: 17056416
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
 
LVL 1

Author Comment

by:kkamm
ID: 17058530
>> 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
ID: 17058793
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 750 total points
ID: 17058893
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
ID: 17072636
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
ID: 17073108
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview

824 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