Solved

unmanaged calling managed fails in DLL

Posted on 2014-04-01
10
832 Views
Last Modified: 2014-05-03
Hi,

I'm attempting to call managed code from unmanaged through a simple DLL interface. Microsoft docs claim (again and again) that this works fine but provide no examples. I set up an extremely simple console app that works fine (below). I put the same tiny bit of code in a DLL, set every compiler setting identical to the console app (CLR on), and it fails. It blows up deep in Windows with an undefined error.

I'm using VS-2010, .NET 4.0, Windows 7 64bit.
Here are the code samples:

Console app that works fine:
// hello.cpp : main project file.
using namespace System;
int write_msg(wchar_t *MSG1,wchar_t *MSG2);

#pragma unmanaged

int main()
{
      write_msg(L"Hello World",L"How is it going?");
      return (0);
}

#pragma managed

int write_msg(wchar_t *MSG1,wchar_t *MSG2)
{
      String^ msg1 = gcnew String(MSG1);
      String^ msg2 = gcnew String(MSG2);
    Console::WriteLine(msg1);
    Console::WriteLine(msg2);
    return 0;
}


Here's the DLL code that fails:
// This is the main DLL file.
using namespace System;
extern "C"
{
      void test_msg(wchar_t*,wchar_t*);
}
int write_msg(wchar_t *MSG1,wchar_t *MSG2);

#pragma unmanaged

void test_msg(wchar_t *msg1,wchar_t *msg2)
{
      write_msg(msg1,msg2);
}

#pragma managed

int write_msg(wchar_t *MSG1,wchar_t *MSG2)
{
      String^ msg1 = gcnew String(MSG1);
      String^ msg2 = gcnew String(MSG2);
      return (0);
}


When debugging, control gets to the unmanaged function test_msg(), and the arguments are correct. The error occurs when test_msg() tries to call write_msg().

A clue, perhaps. In the console app, I can set breakpoints on any line in the managed or unmanaged sections. In the DLL I can set breakpoints only in the unmanaged section.
0
Comment
Question by:PacificaResearch
  • 6
  • 2
10 Comments
 
LVL 12

Expert Comment

by:satsumo
ID: 39972032
The calling code does not call test_msg?
0
 

Author Comment

by:PacificaResearch
ID: 39972675
As I wrote:

When debugging, control gets to the unmanaged function test_msg(), and the arguments are correct. The error occurs when test_msg() tries to call write_msg().
0
 
LVL 12

Expert Comment

by:satsumo
ID: 39972813
The code for the console app calls write_msg(). In the DLL code, write_msg() is not external and it does not call test_msg(). So I don't understand how control can reach test_msg() given the code shown.
0
 

Author Comment

by:PacificaResearch
ID: 39973024
In the DLL, test_msg() is external and unmanaged and called by some outside unmanaged code. That part works fine. Control arrives at test_msg() and the arguments are correct.

There is a prototype for write_msg(), to satisfy the compiler, and test_msg() attempts to call write_msg(), which fails. The problem is between test_msg() (unmanaged) and write_msg() (managed).

In the console app, the main() function is the equivalent of the test_msg() function.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:PacificaResearch
ID: 39979088
I simplified the DLL code to the absolute minimum and it still blows up the same way. Every Microsoft article I read about C++ interop states that mixed-mode DLLs "just work" as long as you compile with /clr. In this sample, I'm not passing any arguments, not returning any result, not executing ANY code except the call to write_msg(), and the blowup is the same as before.

Please, any help at all would be much appreciated!

All of the articles I find on the web or MS knowledgebase seem to pertain to VS-2003 or VS-2005. Did Microsoft remove C++ interop from VS-2010 and Windows 7? The compiler doesn't report any problems with this code, and the debugger steps to the test_msg() function. It can't step to write_msg() without blowing up.

Is it possible for an expert to compile and step this sample to see the result?



// Bridge.cpp - compiles to bridge.dll.
using namespace System;
extern "C"
{
      void test_msg();
}
void write_msg(); // Prototype for write_msg (below)

#pragma unmanaged

void test_msg()
{
      // Debugger stops here without error.
      write_msg();
}

#pragma managed

void write_msg()
{
      // Function does nothing, still blows up.
}
0
 

Author Comment

by:PacificaResearch
ID: 39989558
I really can't make this question any simpler.

The Microsoft documentation, such as it is, clearly states that C++ interop with /clr compiler option allows unmanaged C++ to call managed C# directly, in the same .DLL, using the #pragma managed and #pragma unmanaged operators.

Has anyone ever had any success with this?

Is Microsoft simply lying about their capabilities? Are they smoking something here in Washington state?

Has anyone looked at the very simple sample code I posted above? It compiles fine, and blows up. It doesn't work. It should work, unless the documentation is just not true.

Any help or suggestions would be much appreciated. Thanks!
0
 

Accepted Solution

by:
PacificaResearch earned 0 total points
ID: 40027868
I created two Visual Studio solutions for this issue, one for console and the other for .DLL. They are virtually identical. The console solution works fine. The .dll solution blows up.

I began making changes in the compiler and linker settings for the .exe that calls the initial .dll. I didn't make the changes for any particular reason; the initial settings were already correct. Eventually I came across settings that made the .dll work. When I restored the settings to their original values, the .dll continued to work. When I remade the solution, the .dll blows up. There seems to be something unstable in C++ interop. I'll put up with it until something better comes along.
0
 

Author Closing Comment

by:PacificaResearch
ID: 40039021
No expert had any useful advice.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

861 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

24 Experts available now in Live!

Get 1:1 Help Now