[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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
0
kkamm
Asked:
kkamm
  • 4
  • 3
  • 2
2 Solutions
 
lakshman_ceCommented:
>>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
 
AlexFMCommented:
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
 
kkammAuthor Commented:
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
AlexFMCommented:
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
 
kkammAuthor Commented:
>> 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
 
AlexFMCommented:
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
 
lakshman_ceCommented:
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
 
kkammAuthor Commented:
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
 
kkammAuthor Commented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now