小弟刚学VC不久,现在在学习一个工程的时候卡在一个错误上了,下面是出错的部分代码:
bool CDiCom4K::Init(short ComNo,CString ComSet,CWnd* pParent,int ID)
{
BSTR bStrLicense = ::SysAllocStringLen(pwchLicenseKey,sizeof(pwchLicenseKey)/sizeof(WCHAR));
Create(NULL,0,CRect(0,0,0,0),pParent,ID,NULL,FALSE,bStrLicense);//这个Create() 函数是用来创建一个串口控件的
::SysFreeString(bStrLicense);
SetCommPort(ComNo);
SetSettings(ComSet);
SetInBufferSize(1024);
SetInBufferCount(0);
SetInputMode(1);
SetRThreshold(1);
SetInputLen(0);
...(以下省略)
BOOL CDetect::OnInitDialog()
{
CDialog::OnInitDialog();
RgsPath="Software\\VB and VBA Program Settings\\VehicleDetect\\Database";
m_Vehicle.Clear();
pApp=(CVcleDApp*)AfxGetApp();
if (m_DiCom4K.Init(pApp->ComNo,pApp->ComParam,this,IDC_MSCOMM1))
...(以下省略)
第二段程序代码是工程中调用Init()函数的部分.
程序运行到第一段程序的Create()函数的时候就崩溃了,然后弹出一个对话框
Unhandled exception in **.exe(MFC042D.dll):0xC0000005:Access violation
请问大侠们这是什么错误啊?在线等
bool CDiCom4K::Init(short ComNo,CString ComSet,CWnd* pParent,int ID)
{
BSTR bStrLicense = ::SysAllocStringLen(pwchLicenseKey,sizeof(pwchLicenseKey)/sizeof(WCHAR));
Create(NULL,0,CRect(0,0,0,0),pParent,ID,NULL,FALSE,bStrLicense);//这个Create() 函数是用来创建一个串口控件的
::SysFreeString(bStrLicense);
SetCommPort(ComNo);
SetSettings(ComSet);
SetInBufferSize(1024);
SetInBufferCount(0);
SetInputMode(1);
SetRThreshold(1);
SetInputLen(0);
...(以下省略)
BOOL CDetect::OnInitDialog()
{
CDialog::OnInitDialog();
RgsPath="Software\\VB and VBA Program Settings\\VehicleDetect\\Database";
m_Vehicle.Clear();
pApp=(CVcleDApp*)AfxGetApp();
if (m_DiCom4K.Init(pApp->ComNo,pApp->ComParam,this,IDC_MSCOMM1))
...(以下省略)
第二段程序代码是工程中调用Init()函数的部分.
程序运行到第一段程序的Create()函数的时候就崩溃了,然后弹出一个对话框
Unhandled exception in **.exe(MFC042D.dll):0xC0000005:Access violation
请问大侠们这是什么错误啊?在线等
MFCO42D! 5f50f474()
VCLED! CDiCom4K::Create(char const *,unsigned long,struct tagRECT const &,class CWnd *,unsigned int,class CFile *,int,unsigned short *) + 63 bytes
我用Call stack,运行到上面第一句那就停住了
regsvr32 注册成功,但仍然不能使用,经查找是LicenseKey的问题
只需在创建控件时增加下列几行即可
WCHAR pwchLicenseKey[] =
{
0x0043, 0x006F, 0x0070, 0x0079, 0x0072, 0x0069,
0x0067, 0x0068, 0x0074, 0x0020, 0x0028, 0x0063,
0x0029, 0x0020, 0x0031, 0x0039, 0x0039, 0x0034,
0x0020
};
BSTR bstrLicense = ::SysAllocStringLen(pwchLicenseKey,
sizeof(pwchLicenseKey)/sizeof(WCHAR));
if(!m_ComPort.Create(NULL,0,CRect(0,0,0,0),AfxGetMainWnd(),
IDC_MSCOMM1,NULL,FALSE,bstrLicense))
{
AfxMessageBox("Failed to create OLE Communications Control\n");
return -1; //fail to create
}
......
::SysFreeString(bstrLicense);
以上是我在网上找到的有关这个的资料,代码是之前别人写的,感觉应该是有效的
{
0x0043, 0x006F, 0x0070, 0x0079, 0x0072, 0x0069,
0x0067, 0x0068, 0x0074, 0x0020, 0x0028, 0x0063,
0x0029, 0x0020, 0x0031, 0x0039, 0x0039, 0x0034,
0x0020
};
我的程序里也有这段代码
你可以跟踪一下BSTR bStrLicense的值,看是不是在赋值的时候就出错了,如果是,那可能就是::SysAllocStringLen(pwchLicenseKey,sizeof(pwchLicenseKey)/sizeof(WCHAR));
这一句的问题了
一步步跟着做
不过我有个疑问:我的这个貌似是动态创建串口控件的,跟直接插入控件一样吗?
另外,要不你加一个Button,点击btn的时候再创建,看看有没有问题呐。。
http://hi.baidu.com/zhitenglin/blog/item/14df873d00005c08baa16799.html/cmtid/19c9372e0381175d4fc226bb
希望对你有帮助