C ++ Unmanaged State Call - Class from ASP.NET Web Service

I inherited half of the completed application, which seems to be using a model that I'm not sure can work reliably.

This is an ASP.NET web service that loads unmanaged C ++. DLL on each call using

[DllImport ( "kernel32.dll" , EntryPoint = "LoadLibraryA" )]
public static extern int LoadLibrary( string lpLibFileName );

and then makes several calls, for example,

 [DllImport(@"MyUnamanagedDLL.dll")]
 public static extern string DoStuff( );

Unmanaged C ++. Dll uses a one-point relationship between states between calls. This means that it should only initialize once and load a bunch of slow files from disk and database, and not every web service call, because it is too slow.

Thus, each call to an unmanaged .dll first calls Getinstance (), if the instance is null, it initializes it and reloads everything if it does not assume that it is ready for operation.

FreeLibrary -, , , .

? , - , ? , singleton loadlibrary?

+3
4

? , - , ?

; : ? , ​​ , , HWND - , ​​ , , ( "[I] n , , , ", , , Windows, ).

-, , DllMain, PROCESS_DETACH .

, singleton loadlibrary?

:

  • singleton
  • A singleton,
  • B singleton, , ,
  • A B ,
  • singleton
  • C singleton,
  • ...

:

  • singleton
  • A singleton,
  • A B singleton, ;
  • A B singleton,
  • , Process C , , ;

, , , / ( /) . . Mutex es


; ( ). , . API- C-like ( , CreateXxxObject()/DestroyXxxObject(), struct s, APR) ++. , .

, , ", ,..."

+1

, DLL. , , , , dll .

, dll, , #, ASP.NET .

.NET 2.0 global.asax -, . , - -... global.asax sessionid , , .

protected void Session_Start( object sender, EventArgs e ) 
{ 
  Trace.WriteLine( "Session_Start" ); 
}

Session_Start global.asax , , .. ( dll...). , global.asax, ( , dll)...

0

, DLL, . , P/Invoke - , ( , , , ). , , - DLL Windows, WCF . , , . , .

0

. , COM +, - p/Invoke. p/Invoke, - . , , , Microsoft! , p/Invoke , . , , , /, ASP.Net , , . , .

In response to the question, can an unmanaged cross-language DLL be recalled from managed code using p / Invoke? Yes it can. BUT; it must be designed to maintain its condition in a reentrant manner. This means that the unmanaged DLL must monitor the memory that it allocates and free this memory every time it is called from your web service. It should not free or allocate memory belonging to another web service call.

0
source

Source: https://habr.com/ru/post/1779078/


All Articles