Declare Function CoInitializeSecurity Lib "ole32.dll" (pVoid As IntPtr, _
cAuthSvc As Integer, asAuthSvc() As SOLE_AUTHENTICATION_SERVICE, _
pReserved1 As IntPtr, dwAuthnLevel As Integer, dwImpLevel As Integer, _
pAuthList As IntPtr, dwCapabilities As Integer, pReserved3 As IntPtr) As Integer
You shouldn't call CoInitializeSecurity from managed code. That's because the CLR will almost always call CoInitialize upon startup before execution enters your main method, and CoInitialize will implicitly call CoInitializeSecurity if it hasn't already been called. Therefore, calling this from managed code will usually return RPC_E_TOO_LATE.
The workaround is to write an unmanaged "shim" that will call CoInitializeSecurity, then activate and call into managed code. You can do this via an export from a mixed-mode C++ DLL, by registering a managed component for use by COM, or by using the CLR hosting API.
You can prevent the CLR from calling CoInitializeEx and CoInitializeSecurity automatically on startup, by removing the STAThread or MTAThread attribute from above Main(). You can then explicitly call CoInitializeEx/CoInitializeSecurity with any specific authentication settings you require.
Alternative Managed API:
Do you know one? Please contribute it!
Identifies an authentication service that a server is willing to use to communicate to a client.
3/16/2007 8:20:07 AM - anonymous
Registers security and sets the default security values for the process, but this can't be directly called from managed code.
3/19/2008 7:53:21 AM - RobertChipperfield-212.44.26.236
Registers security and sets the default security values for the process, but this can't be directly called from managed code.
11/17/2010 12:47:58 AM - -71.32.39.4
Registers security and sets the default security values for the process, but this can't be directly called from managed code.
11/17/2010 12:47:58 AM - -71.32.39.4
Registers security and sets the default security values for the process, but this can't be directly called from managed code.
3/19/2008 7:53:21 AM - RobertChipperfield-212.44.26.236
Registers security and sets the default security values for the process, but this can't be directly called from managed code.
3/19/2008 7:53:21 AM - RobertChipperfield-212.44.26.236
CoInitializeEx initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required. Values for the dwCoInit parameter are taken from the COINIT enumeration. However, since pinvoke is a dotNET construct you should be aware that dotNET already does a COM initialization and therefore calling a CoInitializeEx function most likely will not do what you expect. This problem occurs when trying to instantiate a COM object from within dotNET where the COM objects threading model is different from dotNETs. Search on Common Language Runtime or CLR and COINIT_APARTMENTTHREADED to find posts on this issue.
7/25/2015 5:25:25 PM - -5.202.143.191
Registers security and sets the default security values for the process, but this can't be directly called from managed code.
3/19/2008 7:53:21 AM - RobertChipperfield-212.44.26.236