How To Use CoInitializeSecurity in Visual Basic
HOWTO: 创建 DCOM 客户/服务器应用程序
http://support.microsoft.com/kb/161837/zh-cn
How To Use CoInitializeSecurity in Visual Basic
http://support.microsoft.com/default.aspx?scid=kb;EN-US;239561
This article was previously published under Q239561
This article was previously published under Q239561
SUMMARY
The CoInitializeSecurity function can be used to set security values for a process. There are some restrictions that must be considered when calling this function from a Visual Basic application.
MORE INFORMATION
The CoInitializeSecurity function must be called at the very beginning of your Visual Basic program and before any type of marshalling is performed. If you are developing an ActiveX Server application such as an ActiveX EXE, there is no way to successfully call CoInitializeSecurity because marshalling has already been performed when your application gets control. When calling CoInitializeSecurity from a Standard EXE project, you must start your application with a Sub Main and the call must be made at the very beginning of the procedure. Another point to consider is that a call to CoInitializeSecurity only succeeds in the compiled executable. When running an application in the IDE, the function call fails because marshalling has already occurred for the Visual Basic process and therefore it is already too late to make the call. To work around this, use a conditional compilation block when making the function call as shown here.
1. | Open a Standard EXE project. Form1 is created by default. |
2. | From the Project menu, add a new module and create a Sub Main. |
3. | From the Project menu, choose Project1 Properties and define Sub Main as the start-up procedure. |
4. | Include the following Sample Code in the bas module:
|
5. | Make and run the executable.NOTE: If you choose to run the application in the IDE, you must define the RUN_IN_IDE constant as TRUE. Do this as follows: From the Project, menu select Project1 Properties and click the Make tab. In the Conditional Compilation Arguments field, enter RUN_IN_IDE = -1. Be sure to remove the above Conditional Compilation Argument "RUN_IN_IDE" when making the executable. |