• 12
• 12
• 9
• 10
• 13

# [.net] Using native library from managed C++

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been trying to use RakNet in a managed C++ application (I think it's managed... in VC++ 2005 Express I choose "windows form application" and change the clr from /clr:pure to /clr). My code is as follows:
// CLR test.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

#include <RakNetworkFactory.h>  //these are headers from a native library
#include <RakPeerInterface.h>

using namespace CLRtest;

int main(array<System::String ^> ^args)
{
RakPeerInterface * peerInterface = RakNetworkFactory::GetRakPeerInterface(); //native function call

// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}


This compiles and links with no errors and the single warning: RakNetLibStaticDebugMD.lib(RakNetworkFactory.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification However, when I run it, all I get is a default system alert sound (no dialog or error box). The thread does not exit, but no form ever appears. In the debug output is an access violation message: First-chance exception at 0x7c96cc82 in CLR test.exe: 0xC0000005: Access violation reading location 0x3db8bccb. This violation appears to happen before the first line of the main function is reached. If I comment out the //native function call line, the application works as expected, displaying a default form and completely closing when asked to. I feel like this has been answered before but I can't quite piece the clues together. Does anyone have any insight? Thanks, Riley

##### Share on other sites
I may be wrong, but I'm pretty sure you still need to create a wrapper around the unmanaged DLL before you can access it in managed code. At least that's what my findings on Google seem to suggest. [sad]

##### Share on other sites
The second sample listed at http://msdn2.microsoft.com/en-us/library/ms235282.aspx seems to suggest that I don't need to write my own wrapper. Perhaps this is only true for functions, and not true for objects with their own memory structure requirements?

##### Share on other sites
No, you don't have to write a wrapper. Tried stepping into the native call and see what happens?

##### Share on other sites
I can't step into it - the access violation happens before the first line of the main function. Very... unhelpful! I'm completely at a loss. I assume that the executable image is somehow malformed, as its expecting one format and the native code is in another? I don't know :(

Here is the full debug output:

'CLR test.exe': Loaded 'C:\Documents and Settings\SFS User\My Documents\Visual Studio 2005\Projects\CLR test\debug\CLR test.exe', Symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\mscoree.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\ws2_32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\ws2help.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcr80d.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcm80d.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\imm32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727\mscorwks.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_0de06acd\msvcr80.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727\Culture.dll', No symbols loaded.'CLR test.exe': Unloaded 'C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727\Culture.dll''CLR test.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\mscorlib\1240d7945115c7448b6dff50a0a99b20\mscorlib.ni.dll', No symbols loaded.'CLR test.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\uxtheme.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\MSCTF.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\Program Files\Common Files\Microsoft Shared\Ink\tiptsf.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\oleacc.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\msvcp60.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\oleaut32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\psapi.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\version.dll', No symbols loaded.'CLR test.exe' (Managed): Loaded 'c:\documents and settings\sfs user\my documents\visual studio 2005\projects\clr test\debug\CLR test.exe', Symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727\mscorjit.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727\diasymreader.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\rsaenh.dll', No symbols loaded.'CLR test.exe' (Managed): Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcm80d.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System\fc3c04ce0a5bd7489e063c5180384f84\System.ni.dll', No symbols loaded.'CLR test.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', No symbols loaded.First-chance exception at 0x7c96cc82 in CLR test.exe: 0xC0000005: Access violation reading location 0x41417a7d.'CLR test.exe': Loaded 'C:\WINDOWS\system32\MSCTFIME.IME', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\secur32.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\clbcatq.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\comres.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\Program Files\Windows Journal\nbmaptip.dll', No symbols loaded.'CLR test.exe': Loaded 'C:\WINDOWS\system32\xpsp2res.dll', Binary was not built with debug information.

As you can see, the access violation occurs just after the .NET-ey components are loaded...

Yeah, I'm stumped. Thanks for the replies so far~

##### Share on other sites
I've been searching for an answer for this for a couple of days now... I'm about to give up on the whole concept. Surprisingly, I've found several references to the hypothetical possibility of what I want to do, and zero actual examples of it working.

Has anyone successfully done this sort of thing (linking a native non-standard library to a managed C++ app with VS2005 Express)?

Thanks for any scrap of hope you can give me at this point :p

##### Share on other sites
That code should work fine.

If you compile with clr:pure then the compiler will generate interop (dllImport) calls to the external functions, and will wrap up the unmanaged data structuers, etc. However this requires the functions to explicitly state if they are __cdcall, etc. So you may have some compile problems with pure syntax.

My current project I have a wrapper class in C++ express. Although this is using clr:oldsyntax. It works fine, however I have run into problems with the C++ optimiser in express producing bugs in native release code.
I have not tried running native code not compiled in express with managed C++ in express though.

Just because the access violation is occuring after system.dll has been loaded does not mean the error is occuring in, or because of system.dll.

Do you have other versions of visual studio installed? betas or not.
I did have a problem with moving from VS 2005 beta 2 to the full version. After then, any managed C++ app I compiled in VS 2003 (.net 1.1) would have various issues and general weirdness. It got to the stage malloc/new would crash every time.
It turned out that VS 2005 beta had left a bunch of .libs lying about. One such lib was msvcrt.lib, which as you might know is quite important :P somehow VS 2003 had got confused and started using this copy instead during compiles.
The fix was to ingore all default libraries compile option, with explicitly adding msvcrt.lib.

Unfortunatly native code is a trial to work with at times.

##### Share on other sites

That this code should work is what's so frustrating (along with the nearly COMPLETE lack of information about what is actually going wrong or even WHERE it's going wrong). I am compiling with /clr, not /clr:pure or /clr:safe, and I compiled the native code in VS2005 Express as well.

Argh!!

My next step is going to be making a very trivial native library and see if I can get that working from managed code... maybe I can at least narrow this down to a problem in one set of code or the other.

I'll post back here when I find something, assuming I don't just uninstall visual studio and start studying poetry.