我用VC 6.0编写了一个DLL,调用用了一个叫做Snmp++的开发包,这个开发包不是DLL形式的,全部是源代码形式的,基本上都是“.h”和“.cpp”的文件,我是通过在VC的“Tools->Directories”中的“Include files”把开发包中的一个名为“include”的文件夹导入进来,然后就可以使用这个开发包了,这也是snmp++官方提供的调用方式。现在问题是,我在DLL中调用了一个开发包中的方法,这个方法属于一个类。代码是这样的:
Snmp::socket_startup();
它的作用是建立一个Snmp的链接。
然后我做了一个程序,调用这个DLL测试,现在可以肯定的是,程序是运行到这里出的错。另外,我将这个DLL改成了一个命令行程序,测试没有任何问题。我就是觉得是不是编写DLL时,如果调用第三方的开发包的话,必须要遵循一些规范。向各位请教了!谢谢!
Snmp::socket_startup();
它的作用是建立一个Snmp的链接。
然后我做了一个程序,调用这个DLL测试,现在可以肯定的是,程序是运行到这里出的错。另外,我将这个DLL改成了一个命令行程序,测试没有任何问题。我就是觉得是不是编写DLL时,如果调用第三方的开发包的话,必须要遵循一些规范。向各位请教了!谢谢!
因为是DLL,没法调试,我是用VC 6.0自己写了一个调用程序调用这个Dll,出错信息就是通常程序报错的样子,是Windows自己的,什么“发送错误报告”的那个。没什么实际意义。我之所以确定错误在这里,是我将Dll里面的代码一个功能一个功能依次注释掉测试出来的
#pragma warning (disable:4786)
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>
#include "snmp_pp.h"#pragma comment(lib,"snmpGQ.lib")
#pragma comment(lib,"WS2_32.LIB")using namespace std;ofstream fout("scan-report.xml");//---------------获取txt中的oid号--------------
string getOid(string s)
{
int pos_minus;
pos_minus=s.find_first_of('-',0);
return s.substr(0,pos_minus);
}//--------------获取txt中的description-------------
string getDes(string s)
{
int pos_minus;
pos_minus=s.find_first_of('-',0);
return s.substr(pos_minus+1,s.length());
}//----------------获取txt中oid----------void readTxt(vector<string> *content)
{
vector<string> &_content=*content;
ifstream fin("oid.ini");
string temp;
while(getline(fin,temp,'\n'))
{
_content.push_back(temp);
}
fin.close();
}//-----------------获取系统日期和时间----------------void getDateTime()
{
time_t curtime=time(0);
tm tim =*localtime(&curtime);
int sec,min,hour,day,mon,year;
sec=tim.tm_sec;
min=tim.tm_min;
hour=tim.tm_hour;
day=tim.tm_mday;
mon=tim.tm_mon;
year=tim.tm_year; fout<<year+1900<<"-"<<mon+1<<"-"<<day<<" "<<hour<<":"<<min<<":"<<sec;
}//-----------------snmpv1和v2版本获取信息 ipaddr为目标地址、cmu为community-----------------------bool snmpConnect(const char* ipaddr,const char* cmu)
{
UdpAddress address(ipaddr);
if ( !address.valid())
{
return false;
}
Snmp::socket_startup();
int status;
OctetStr community(cmu);
CTarget ctarget((IpAddress)ipaddr);
ctarget.set_readcommunity(community);
Vb vb;
Snmp snmp(status);
vb.set_oid("1.3.6.1.2.1.1.1.0");
Pdu pdu;
pdu += vb;
if (status != SNMP_CLASS_SUCCESS||snmp.get(pdu, ctarget) != SNMP_CLASS_SUCCESS)
{
Snmp::socket_cleanup();
return false;
}
Snmp::socket_cleanup();
return true;
}bool snmpGet(const char* ipaddr,string pwd)
{
const char* cmu = pwd.data();
if(snmpConnect(ipaddr,cmu)==false)
{
return false;
}
else
{
fout<<"<item>\n";
fout<<"<ip-address>"<<ipaddr<<"</ip-address>"<<"\n";
//--------获取txt信息------
vector<string> content;
readTxt(&content);
string str,oidstr,desstr;
char * result="here";
vector<string>::iterator p=content.begin();
//--------获取信息-----------
Snmp::socket_startup();
int status;
OctetStr community(cmu);
CTarget ctarget((IpAddress)ipaddr);
ctarget.set_readcommunity(community);
Vb vb;
Snmp snmp(status);
for(int j=0;j<content.size();j++)
{
str=*p;
oidstr=getOid(content[j]);
desstr=getDes(content[j]);
vb.set_oid(oidstr.data());
Pdu pdu;
pdu += vb;
if ( (status = snmp.get( pdu, ctarget)) != SNMP_CLASS_SUCCESS)
{
if(desstr=="sysDescr")
{
fout<<"<system_descrption></system_descrption>\n";
} if(desstr=="sysName")
{
fout<<"<hostname></hostname>\n";
} if(desstr=="sysObjectID")
{
fout<<"<sysObjectID></sysObjectID>\n";
}
}
else
{
pdu.get_vb(vb,0);
if(desstr=="sysDescr")
{
fout<<"<system_descrption>"<<vb.get_printable_value()<<"</system_descrption>\n";
} if(desstr=="sysName")
{
fout<<"<hostname>"<<vb.get_printable_value()<<"</hostname>\n";
} if(desstr=="sysObjectID")
{
fout<<"<sysObjectID>"<<vb.get_printable_value()<<"</sysObjectID>\n";
}
}
}
fout<<"</item>\n";
Snmp::socket_cleanup();
}
return true;
} int main(int argc, char *argv[])
{
vector<string> ips;
for(int k=2;k<argc;k++)
{
ips.push_back(argv[k]);
}
const char* ip="";
fout<<"<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n";
fout<<"<network-scanner-results>\n";
fout<<"<summary>Network Scanner Report on ";
getDateTime();
fout<<"</summary>\n";
fout<<"<items>\n";
for(int i=0;i<ips.size();i++)
{
ip=ips[i].data();
snmpGet(ip,argv[1]);
ip="";
}
fout<<"</items>\n";
fout<<"</network-scanner-results>\n";
fout.close();
return 0;
}
DLL也可以调试,你都用debug跟进去看看到底什么地方crash了
Unhandled execption in PropertyScanTest.exe(NTDLL.DLL):0xC0000005:Access Violation.Debug中的信息如下:
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded 'G:\PropertyScanTest\Debug\PropertyScan.dll', no matching symbolic information found.
Loaded symbols for 'E:\WINDOWS\system32\MSVCP60D.DLL'
Loaded symbols for 'E:\WINDOWS\system32\MSVCRTD.DLL'
Loaded 'E:\WINDOWS\system32\iphlpapi.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\secur32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\user32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\ws2_32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\ws2help.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\wsock32.dll', no matching symbolic information found.
Loaded symbols for 'E:\WINDOWS\system32\MFC42D.DLL'
Loaded symbols for 'E:\WINDOWS\system32\MFCO42D.DLL'
Loaded 'E:\WINDOWS\system32\imm32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\lpk.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\usp10.dll', no matching symbolic information found.
Loaded 'E:\Program Files\Google\Google Desktop Search\GoogleDesktopNetwork3.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\mfc42loc.dll', no matching symbolic information found.
The thread 0x948 has exited with code 0 (0x0).
Loaded 'E:\WINDOWS\system32\comctl32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\uxtheme.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\shell32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\shlwapi.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\ole32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\oleaut32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_6b128700\msvcr80.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_6b128700\msvcp80.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\version.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83\comctl32.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\WinSxS\x86_Microsoft.VC80.MFCLOC_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_3415f6d0\mfc80CHS.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\setupapi.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\msctf.dll', no matching symbolic information found.
Loaded 'E:\WINDOWS\system32\msctfime.ime', no matching symbolic information found.
First-chance exception in PropertyScanTest.exe (NTDLL.DLL): 0xC0000005: Access Violation.
The program 'G:\PropertyScanTest\Debug\PropertyScanTest.exe' has exited with code 0 (0x0).
用它自带的project。编译成Lib,然后在你的Project中导入,接着调用,看它自带的ReadMe