473,396 Members | 1,777 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ASP.NET Web API calling unmanaged Dll hanging

Hi all,

I'm trying to dynamically call unmanaged C++ Dll from within my Web API. For this reason, I created 2 Controller Action:

(1) One is starting the dll: It uses the LoadLibrary to load the dll, and call an init-method in the dll, initializing some parameters for calculation.

(2) Here's is were I need the calculation result from my Dll (using the handle from LoadLibrary in Action 1, I use GetProcAdress to locate the function and create the delegate).

It seems to work fine, but when calling it multiple times, it hangs up - and this very sporadically and unpredictable. When I pause the debugging with Visual Studio, it is paused at my delegate-call for the function...

Can someone please help me?

Here's my code:
Expand|Select|Wrap|Line Numbers
  1. [DllImport("kernel32.dll", SetLastError = true)]
  2. private static extern IntPtr LoadLibrary(string dllToLoad);
  3.  
  4. [DllImport("kernel32.dll", SetLastError = true)]
  5. private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
  6.  
  7.  
  8. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  9. private delegate bool DllTransfer(ref WebCalcInfo Info);
  10.  
  11. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  12. private delegate bool InitDll([MarshalAs(UnmanagedType.LPStr)]string pExternPath);
  13.  
  14.  
  15. //This method is called in 1. Action
  16. public static bool LoadInitLibrary(string path)
  17.         {
  18.             if (!IsLoaded)
  19.             {
  20.                 PATH = path;
  21.                 if (!Directory.Exists(PATH) && !File.Exists(PATH_DLL))
  22.                     throw new Exception(string.Format("Dll not found: {0}.", PATH_DLL));
  23.  
  24.                 //LoadLibrary
  25.                 hModule = DllInterop.LoadLibrary(PATH_DLL);
  26.                 IsLoaded = (hModule != IntPtr.Zero);
  27.                 if (!IsLoaded)
  28.                 {
  29.                     int errno = Marshal.GetLastWin32Error();
  30.                     throw new Exception(string.Format("Failed to load library. Error:{0}.", errno));
  31.                 }
  32.  
  33.                 //GetPointer: PROC_InitDll
  34.                 IntPtr intPtrInitDll = DllInterop.GetProcAddress(hModule, PROC_InitDll);
  35.                 if (intPtrInitDll == IntPtr.Zero)
  36.                 {
  37.                     int errno = Marshal.GetLastWin32Error();
  38.                     FreeLibrary();
  39.                     throw new Exception(string.Format("Process Address not found: PROC_InitDll is IntPtr.Zero. Error:{0}.", errno));
  40.                 }
  41.                 DllInterop.InitDll initDll = (DllInterop.InitDll)Marshal.GetDelegateForFunctionPointer(intPtrInitDll, typeof(DllInterop.InitDll));
  42.  
  43.                 //InitDLL
  44.                 IsInitialized = initDll(string.Format("P={0}", PATH));
  45.                 if (!IsInitialized)
  46.                 {
  47.                     FreeLibrary();
  48.                     throw new Exception("PROC_InitDll returned FALSE.");
  49.                 }
  50.  
  51.                 return true;
  52.             }
  53.             return false;
  54.         }
  55.  
  56. //This method is for the 2.Action
  57. public static CalcInfo TransferData(WebCalcInfo calcInfo)
  58.         {
  59.             if (!IsLoaded && !string.IsNullOrEmpty(PATH))
  60.             {
  61.                 System.Diagnostics.Trace.WriteLine("Library was not loaded, start loading and init Dll.", "TransferData");
  62.                 LoadInitLibrary(PATH);
  63.             }
  64.  
  65.             IntPtr intPtrTransfer = DllInterop.GetProcAddress(DllInterop.hModule, PROC_DllTransfer);
  66.             if (intPtrTransfer == IntPtr.Zero)
  67.             {
  68.                 int errno = Marshal.GetLastWin32Error();
  69.                 FreeLibrary();
  70.                 throw new Exception(string.Format("Process Address not found: PROC_DllTransfer is IntPtr.Zero. Error:{0}.", errno));
  71.             }
  72.             DllInterop.DllTransfer dllTransfer = (DllInterop.DllTransfer)Marshal.GetDelegateForFunctionPointer(intPtrTransfer, typeof(DllInterop.DllTransfer));
  73.  
  74.             bool transferResult = dllTransfer(ref Info); // HERE IT HANGS
  75.  
  76.             if (!transferResult)
  77.             {
  78.                 FreeLibrary();
  79.                 throw new Exception("PROC_DllTransfer returned FALSE.");
  80.             }
  81. }
  82.  
Dec 3 '15 #1
2 2693
madankarmukta
308 256MB
Can you please put the complete code here (WebCalcInfo ) that will help more to debug on the problem.
Dec 6 '15 #2
It has nothing to do with WebCalcInfo: I even tried without passing the struct, but same behaviour...
Dec 7 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
4
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage...
9
by: Andrew Cumming | last post by:
I am having trouble using Platform Invoke to call C++ DLLs from C# windows applications. The DLLs seem to load, but I get run-time errors to the effect that the named entry points cannot be found....
4
by: Ed Debrot | last post by:
This seems incredibale to me but I've done some testing and it appears I can't pass in a long and than an int to a function. The int is not passed in correctly. extern static int...
1
by: Adam Clauss | last post by:
I have an unmanaged C++ library that I need to use through C#. I created a Managed C++ class library and through it wrote a wrapper class to call what I need. However, one of the methods (an...
0
by: monika.saxena | last post by:
Hi all, In one of my projects which is a web based application in asp.net, a third party tool - "Frontline Solver DLL" (It is an unmanaged DLL and ..NET is calling it using the PInvoke) is used....
1
by: Leftie | last post by:
Folks, I'm trying to call an unmanaged function from VB.NET and keep getting "Object reference not set to an instance of an object" error. The code that i wrote can be found at:...
6
by: guxu | last post by:
I have a managed C++ code which calls some methods in unmanaged C++ DLL. In my unmanaged DLL, I have a PROTECTED virutal function in a base class and derived class has a PRIVATE function which...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: Markus Eßmayr | last post by:
Hello! I'm writing a .NET component that forwards calls to it's functions on to an unmanaged DLL. This DLL is loaded dynamically using LoadLibrary and the function pointers are retrieved using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.