delphi调用vc++dll中的线程问题,请各位老大帮助!(150分)

  • 主题发起人 主题发起人 四界辰砂
  • 开始时间 开始时间

四界辰砂

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟要将一个带线程类的vc++制作成dll用delphi调用,但是调用后在退出时总是提示acess
violation。。。。。什么的请各位老大看看到底是那方面有问题那?还请各位不吝赐教!
 
DLL本身有问题没?比如用VC++的EXE调用.
退出时最好显式地调用一下FreeLibrary.
 
我是这样调用的,在dll中声明一个全局函数,启动线程,然后在程序退出时的on close事件中
调用在dll中声明的结束线程的函数,在调试时我是执行了线程结束的部分的,但在结束之后
就发生了上面的错误。。。
 
你是怎么结束线程的?
<说我灌水??>
 
字数太少就是灌水?

 
procedure TMainForm.FormClose(Sender: TObject;
var Action: TCloseAction);
var
ThState:Boolean;
begin
ThState:=true;
EndthreadEX(1);
while ThState=truedo
begin
ThState:=GetThreadAlive(1);
end;
end;

而在vc中endthread是这样写的
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE("Thread ended/n");
delete [] m_szWriteBuffer;

m_shutdownevent是关闭线程的事件,GetThreadAlive是我写的检测标志位m_bThreadAlive的程序
请各位大哥看看上面的问题在那里?
 
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
看着很别扭!
没看到改变m_bThreadAlive值的代码.
 
少贴了一段,对不起,
在线程里还有这样一段代码,(完全贴出来可能太长,只贴这一段罗)
Event = WaitForMultipleObjects(3, port->m_hEventArray, FALSE, INFINITE);
switch (Event)
{
case 0:
{
// Shutdown event. This is event zero so it will be
// the higest priority and be serviced first.
port->m_bThreadAlive = FALSE;

// Kill this thread. break is not needed, but makes me feel better.
AfxEndThread(100);
break;
}
case 1: // read event
{
GetCommMask(port->m_hComm, &amp;CommEvent);
if (CommEvent &amp;
EV_CTS)
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent &amp;
EV_RXFLAG)
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent &amp;
EV_BREAK)
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent &amp;
EV_ERR)
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent &amp;
EV_RING)
::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);

if (CommEvent &amp;
EV_RXCHAR)
// Receive character event from port.
ReceiveChar(port, comstat);

break;
}
case 2: // write event
{
// Write character event from port
WriteChar(port);
break;
}
} // end switch
} // close forever loop
这是我从msdn里找的一个串口例子,想要将其做成dll。。。
 
port->m_hEventArray[0]处于通知状态则线程结束.它是什么?m_hShutdownEvent?
EndThread函数中:
SetEvent(中止事件);
WaitForSingleObject(线程名柄,INFINITE);
//比用m_bThreadAlive 好
返回;
 
m_hEventArray[0]就是m_hShutdownEvent
另,名柄是句柄吗?
 
句柄!打错了!
 
后退
顶部