How can I control when a DLL is detached - c#

I got a DLL that I want to add to my C# project and I have some problems. First, my DLL is coded in C++ and I got an interface of one function to export it.
extern "C" __declspec(dllexport) char* sniff()
{
return ps.Sniff();
}
I have an instance "ps" that initialise a socket when the DLL is attached. The point is that I need to have this instance initialise when I call my exported function. My problem is when I import it in my C# project, my DLL is detached for no apparent reason and I can no longer call my exported function.
I use this syntax in my C# projet :
[DllImport(#"C:\Documents and Settings\Pat\Bureau\sniffoporn\Release\sniff.dll", EntryPoint = "sniff", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr sniff();
Some important things that I tryed :
Import the DLL in the reference of the project : Can't because it's not a COM component.
Manually import the DLL using LoadLibrary, FreeLibrary and GetProcAddress : Same result
Call my exported function in loop : Same thing but the DLL is attached for a little longer before detaching.
I would want to know why my DLL is detaching and how can I keep it attached for the rest of the process life.
Thanks a lot

Maybe an exception occurs in your DLL itself. Making it detach in Visual Studio to prevent your application from crashing. After all C# is managed code.
Try your same procedures with a different (dummy) dll with very simple functionality. If it still occurs then something with your method is wrong, otherwise with the DLL you are attempting to use.

Related

Unable to import c++ dll to c# project using DllImport

I'm trying to import a C++ Project Dll into a C# project. I found a lot of people talk about using DllImport. I tried using that and here is what I have-
CPP Code:
int __declspec(dllexport) beginCode(double reportId);
C# Code:
[DllImport("C:\\Users\\<my_user_id>\\Desktop\\ctxmix\\Release\\ctxmix.dll",CallingConvention =CallingConvention.Cdecl ,CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int beginCode(double reportId);
int result = beginCode(reportId);
But when I run, I'm getting an exception - Exception thrown:
System.DllNotFoundException
Do I have to add any references for the CPP Dll in the project or do anything else apart from the code which I have on the top?
Edit: I'm trying to run my .exe using VS2015 and I get this exception on my local machine. Also, I don't see my CPP Dll in the Project->References section where as I see other references there.
The unmanaged DLL needs to be locateable by your managed process. Typically that means placing the DLL in the same directory as the executable file. But you gave used an absolute path which I presume you transcribed correctly.
You may also encounter this error if the DLL's dependencies cannot be located. That seems the likely explanation here. Most likely the MSVC runtime cannot be located when your DLL is loaded.
Using an absolute path isn't a great idea. That will break down when you distribute to another machine. Use just the DLL file name and place it in the same directory as the executable.
Your DllImport attribute seems fussy. No point specifying CharSet when there is no text. I doubt your function calls SetLastError. And do you really need ExactSpelling?

Can't use/debug unmanaged DLL from C#

I have a C# application and a C++ DLL, both x86. The Application is the startup project, the DLL project is inside the same solution and is referenced as a project. The C++ DLL outputs its PDB file with the same name as the DLL inside the Debug folder of the Application.
I have a function called SomeFunction that I'm trying to execute in C#. When the code reaches that line, it stops there. The UI of the C# application continues to be responsive, but the breakpoint never leaves that line (and that's in Form_Load).
If I try to set the DLL as startup project and tell it to execute the C# application, then C# will crash at that line with: System.EntryPointNotFoundException: Unable to find an entry point named 'SomeFunction' in DLL 'SomeDLL.dll'.
This is the declaration of the function that I'm trying to call:
[DllImport("SomeDLL.dll", EntryPoint = "SomeFunction", CallingConvention = CallingConvention.Cdecl)]
public static extern int SomeFunction(IntPtr hwnd);
This is the function's declaration from the C++ header:
#define MYDLL_API __declspec(dllexport)
MYDLL_API extern int SomeFunction(HWND hWnd);
This is how I call it from C#:
var someAnswer = SomeFunction(_hwnd);
UPDATE: since the discussion below could take some time to read, here's the answer in the nutshell: I was missing an extern "C". Also, in order to debug the DLL (which was also a problem), the project needs to support native debugging both from C++ and C#, here's an excellent list that I went through and at the end it was all good!
No Symbols loaded in mixed C# C(win32) project using VS2010
Run code that calls SomeFunction to ensure unmanaged .dll is loaded
Go into Debug -> Windows -> Modules and check the .dll path and symbols status. You can try to load symbols in context menu of your c++ dll in that window.
(very often happens that .net loads different (not expected) version of unmanaged .dll)
Now I'm pretty sure that the problem is that your exported function name is mangled.
Try to define function like this:
extern "C" MYDLL_API int PASCAL SomeFunction(HWND hWnd);
In C#:
[DllImport("SomeDLL.dll")]
public static extern int SomeFunction(IntPtr hwnd);
I suspect the problem that it won't set the breakpoint beforehand is that the DLL is loaded on demand via p-invoke. If VS doesn't think the DLL is a dependency, it may not allow you to set the breakpoint beforehand.
You could add a DebugBreak to your c++ code. Then simply run your app, don't debug it. Then when it executes this statement, you will get a Just in time debugging alert allowing you to jump into the c++ debugger.
C++ as startup
Alternatively, you could make the DLL the startup project, but change the debug settings so that it launches an external process - in this case your app .exe.
This time however, you don't need the DebugBreak(), just a regular breakpoint.
Now when you debug, your DLLs symbols are loaded thus allowing the break points to work.
I used this trick a lot with ActiveX/COM.

How to use a C++ dll in Unity3D?

I am aware of this similar question, but it does not respond to my problem.
I have written two .dlls using Visual Studio 2010. One is in C++, and communicates with an SDK that was written in C++. The other is a C# wrapper for that C++ library, so that it can be used in C# contexts.
My plan was that this would let me use my code in Unity3D, but apparently that is not the case. It seems like Unity3D does not allow me to import .dlls as Assets if they are not a .NET assembly. So I can add my C# wrapper, but not the C++ dll.
This results in a DllNotFoundException whenever I try to access the C++ library. I have tried simply copying the C++ library into the Assets/Plugins folder, but that gives the same results.
Is there a way to do this properly? This is a very vital part of my project setup.
The problem is that the DLL is not being found when the p/invoke runtime code calls LoadLibrary(YourNativeDllName).
You could resolve this by making sure that your DLL is on the DLL search path at the point where the first p/invoke call to it is made. For example by calling SetDllDirectory.
The solution that I personally prefer is for your managed code to p/invoke a call to LoadLibrary passing the full absolute path to the native DLL. That way when the subsequent p/invoke induced call to LoadLibrary(YourNativeDllName) is make, your native DLL is already in the process and so will be used.
internal static class NativeMethods
{
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr LoadLibrary(
string lpFileName
);
}
And then somewhere in your code:
private static IntPtr lib;
....
public static void LoadNativeDll(string FileName)
{
if (lib != IntPtr.Zero)
{
return;
}
lib = NativeMethods.LoadLibrary(FileName);
if (lib == IntPtr.Zero)
{
throw new Win32Exception();
}
}
Just make sure that you call LoadNativeDll passing the full path to the native library, before you call any of the p/invokes to that native library.
Note that the DllNotFoundException can be caused by building your Unity DLL in Debug instead of Release!
A simple oversight that can cause a headache.
This also happens when Unity can find your DLL, but is not able to find it's dependencies. Obvious fix is to place dependency DLLs into /Plugins as well, or link your dependencies statically.
Less obvious reason is when your DLL depends on Visual Studio runtime library dynamically, i.e. is built with Properties -> C/C++ -> Code Generation -> /MD option. Change it to /MT to link with runtime statically.

P/Invoke Runtime Errors

I am trying to create a managed wrapper around a C++ dll. When I try to run a test app, I get an error saying loading the dll failed. This turns out to be a problem with trying to find the CRT. Copying the CRT to the same directory moves past this problem, but then throws an error saying the application loaded the C Runtime incorrectly (R6034). A native C++ program has no trouble with either of these issues.
The dll was compiled with vc 2008 - the managed code is compiled in 2010.
The dll has a manifest and I have tried copying the exact version of the CRT into the working directory with the same result as above.
My questions are :
Why can the native program load the CRT, but the managed wrapper can't find it?
Where could I look for things to try to fix the second error about loading the CRT?
Could this be because of name mangling, CallingConvention, etc?
C++ Code (I only have the header):
class Foo {
public:
static void startCall(std::string hostname);
}
C# Code:
[DllImport("mydll.dll")]
public static extern void startCall(string hostname );
public Test()
{
string hostname = "";
startCall(hostname);
}
As far as I know it is impossible to have STL in function interfaces if you want to use the functions in C#.
If you can change the C++ library interface, change it to use char* instead of strings. If you can't write a proxy library which exposes the interface with char* and converts it to std::string and then calls the c++ library which has functions with std::string

Importing Function From Unmanaged DLL in C# Application

I need to import a function that I wrote in C++, and is now compiled into a DLL, into my C# application. Everything builds without errors or warnings, but when I step through the code the first function call into the DLL throws an Exception with a message of "Unable to find an entry point named 'CreateScanEngine" in DLL 'WMIQuery.dll'." The function is declared like so in my C# application:
internal static class WMIQuery
{
[DllImport("WMIQuery.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
internal static extern void CreateScanEngine();
}
Dependency Walker shows the function there in the DLL as such:
Ordinal: 1(0x0001)
Hint: 0(0x0000)
Function ^: void CreateScanEngine(void)
Entry Point: 0x00001860
Dependency Walker also shows these errors/warnings for the DLL:
Error: At least one required implicit or forwarded dependency was not found.
Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.
Will this make a difference? Also, I tried adding the DLL as a reference to the C# project, and I got this error:
A reference to [my DLL] could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
Does anyone know what I'm doing wrong? Thanks.
Don't use Dependency Walker, it unmangles the exported name of the function and doesn't tell you the real export name. Note that it is smart enough to see that it takes no arguments and has no return value. It can only do this if it sees the name as decorated by the C++ compiler.
Use Dumpbin.exe /exports on the DLL to see the real name. It ought to be "?CreateScanEngine##YGXXZ", use the EntryPoint property in the [DllImport] attribute. You may also want to declare the function with extern "C" so this name mangling doesn't happen.
How are you declaring CreateScanEngine in your C++ code?
Try changing to:
extern "C" __declspec(dllexport) void __stdcall CreateScanEngine();

Categories

Resources