我正在写一个网络程序。它在Windows防火墙及其端口号中打开了一个端口。我创建了一个dll文件,其中包含修改防火墙的所有代码。在这个程序中我使用的是dll。当我第一次执行此操作时,程序在卸载库之前崩溃。它没有显示“卸载防火墙DLL”...但是,如果我再试一次,那么它工作正常(允许程序,并在第一次尝试时在防火墙中打开端口。)
以下是代码和输出:
int Load_DLL(void)
{
TCHAR szPath[MAX_PATH] = {};
GetModuleFileName(NULL,szPath,MAX_PATH);
HINSTANCE Lib;
WIN_FIREWALL addr;
Log("Loading Firewall DLL ...");
Lib = LoadLibrary(FIREWALL_DLL);
if(Lib != NULL)
{
addr = (WIN_FIREWALL)GetProcAddress(Lib,"Win_Firewall");
if(NULL != addr)
{
struct ManipFW mf = {};
mf.OPCODE = ADDPORT;
lstrcpy(mf.PortName,L"MSNet_Spool");
mf.Port = PORT_I;
(*addr)(mf,Log);
memset(&mf,0,sizeof(mf));
mf.OPCODE = ADDAPP;
lstrcpy(mf.AppPath,szPath);
lstrcpy(mf.AppName,L"MS_NET");
(*addr)(mf,Log);
}
else
Log("Firewall GetProcAddress Failed \n");
Log("Unloading Firewall DLL ...");
FreeLibrary(Lib);
}
else
{
Log("Firewall DLL Load failed");
}
return 0;
}
首次尝试输出:
Loading Firewall DLL ...§4Port is not open in the firewall.
Port is now open in the firewall.
Authorized application is disabled in the firewall.
Authorized application is now enabled in the firewall.
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
后续尝试的输出:
Loading Firewall DLL ...í Port is open in the firewall.
Authorized application is enabled in the firewall.
Unloading Firewall DLL ...
如果我在管理员模式下使用命令promt执行程序,则会出现这种情况。但是,如果我在VS2008中使用调试器(使用管理员权限运行),从第一次尝试开始一切正常。