开始我用类做的MFC 扩展DLL调试通过,可不能在另外的WIN32语言下调用,我想改为MFC静态连接,请问这样能在别的语言下用吗?
我试着做了可在向串口写字符的时候一直出错。源代码如下:
请大家指教。!!!!!!!!!
我试着做了可在向串口写字符的时候一直出错。源代码如下:
请大家指教。!!!!!!!!!
解决方案 »
- 删掉一对话框后 运行不了
- 怎么实现像UltraEdit-32里的代码折叠功能和函数列表功能
- 晕了,我在Visual 2005 里边用 sendto函数。有一个参数是char *类型的,但CString.GetBuffer()返回的是wchar_t。我用强制转换为char*
- 视频预览
- 一个MDI的问题
- 请问有没有汉化 dll 文件里面字符串的工具?
- 请问如何用VB实现打印预览?
- 需要开发人员:IC卡门禁消费、停车场管理、短信软件开发、管理软件开发(JAVA,JSP)。
- 送分问题,怎么样动态改变数组大小?
- 是否可以把第三方动态库代码,静态链接进exe。
- 那位高手可以讲解一下如何在ATL中创建一下联结点,--在线提问,急急急
- http://www.180cm.com/download/new/download.asp?id=452&downid=1怎么用网络快车下载
// ComDll.h : main header file for the COMDLL DLL
//#if !defined(AFX_COMDLL_H__AC8D3268_2F76_11D7_9805_00E04CE2E2F1__INCLUDED_)
#define AFX_COMDLL_H__AC8D3268_2F76_11D7_9805_00E04CE2E2F1__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif#define WM_COMM_BREAK_DETECTED WM_USER+1
#define WM_COMM_CTS_DETECTED WM_USER+2
#define WM_COMM_DSR_DETECTED WM_USER+3
#define WM_COMM_ERR_DETECTED WM_USER+4
#define WM_COMM_RING_DETECTED WM_USER+5
#define WM_COMM_RLSD_DETECTED WM_USER+6
#define WM_COMM_RXCHAR WM_USER+7
#define WM_COMM_RXFLAG_DETECTED WM_USER+8
#define WM_COMM_TXEMPTY_DETECTED WM_USER+9/////////////////////////////////////////////////////////////////////////////
// CComDllApp
// See ComDll.cpp for the implementation of this class
//BOOL InitPort(CWnd* pPortOwner, UINT portnr = 1, UINT baud = 19200, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR | EV_CTS, UINT nBufferSize = 512);
void ProcessErrorMessage(char* ErrorText);
BOOL StartMonitoring();
static UINT CommThread(LPVOID pParam);
static void ReceiveChar(COMSTAT comstat);
void WriteToPort(char* string);
static void WriteChar();// 线程定义
CWinThread* m_Thread;// synchronisation objects
CRITICAL_SECTION m_csCommunicationSync;
BOOL m_bThreadAlive;// 句柄
HANDLE m_hShutdownEvent;
HANDLE m_hComm;
HANDLE m_hWriteEvent;// 事件数组.
HANDLE m_hEventArray[3];// 结构定义
OVERLAPPED m_ov;
COMMTIMEOUTS m_CommTimeouts;
DCB m_dcb;// owner window
CWnd* m_pOwner;// misc
UINT m_nPortNr;
char* m_szWriteBuffer;
DWORD m_dwCommEvents;
DWORD m_nWriteBufferSize;class CComDllApp : public CWinApp
{
public:
CComDllApp();// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CComDllApp)
//}}AFX_VIRTUAL //{{AFX_MSG(CComDllApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_COMDLL_H__AC8D3268_2F76_11D7_9805_00E04CE2E2F1__INCLUDED_)
//#include "stdafx.h"
#include "ComDll.h"
#include <assert.h>#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
///////////////////////////////////////////////////////////////////////////////
// CComDllAppBEGIN_MESSAGE_MAP(CComDllApp, CWinApp)
//{{AFX_MSG_MAP(CComDllApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CComDllApp constructionCComDllApp::CComDllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance}/////////////////////////////////////////////////////////////////////////////
// The one and only CComDllApp objectCComDllApp theApp;UINT CommThread(LPVOID pParam)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); m_bThreadAlive = TRUE;
// Misc. variables
DWORD BytesTransfered = 0;
DWORD Event = 0;
DWORD CommEvent = 0;
DWORD dwError = 0;
COMSTAT comstat;
BOOL bResult = TRUE;
// Clear comm buffers at startup
if (m_hComm) // check if the port is opened
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); // begin forever loop. This loop will run as long as the thread is alive.
for (;;)
{
bResult = WaitCommEvent(m_hComm, &Event, &m_ov); if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
// This is a normal return value if there are no bytes
// to read at the port.
// Do nothing and continue
break;
}
case 87:
{
// Under Windows NT, this value is returned for some reason. break;
}
default:
{
ProcessErrorMessage("WaitCommEvent()");
break;
}
}
}
else
{
bResult = ClearCommError(m_hComm, &dwError, &comstat); if (comstat.cbInQue == 0)
continue;
} // end if bResult // Main wait function. This function will normally block the thread
// until one of nine events occur that require action.
Event = WaitForMultipleObjects(3, 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. m_bThreadAlive = FALSE;
// Kill this thread. break is not needed, but makes me feel better.
AfxEndThread(100);
break;
}
case 1: // read event
{
GetCommMask(m_hComm, &CommEvent);
if (CommEvent & EV_CTS)
::SendMessage(m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) m_nPortNr);
if (CommEvent & EV_RXFLAG)
::SendMessage(m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) m_nPortNr);
if (CommEvent & EV_BREAK)
::SendMessage(m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) m_nPortNr);
if (CommEvent & EV_ERR)
::SendMessage(m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) m_nPortNr);
if (CommEvent & EV_RING)
::SendMessage(m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) m_nPortNr);
if (CommEvent & EV_RXCHAR)
// Receive character event from port.
ReceiveChar(comstat);
break;
}
case 2: // write event
{
// Write character event from port
WriteChar();
break;
} } // end switch } // close forever loop return 0;
}void WriteChar()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); BOOL bWrite = TRUE;
BOOL bResult = TRUE; DWORD BytesSent = 0; ResetEvent(m_hWriteEvent); // Gain ownership of the critical section
EnterCriticalSection(&m_csCommunicationSync); if (bWrite)
{
// Initailize variables
m_ov.Offset = 0;
m_ov.OffsetHigh = 0; // Clear buffer
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
bResult = WriteFile(m_hComm, // Handle to COMM Port
m_szWriteBuffer, // Pointer to message buffer in calling finction
strlen((char*)m_szWriteBuffer), // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&m_ov); // Overlapped structure // deal with any error codes
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
// continue to GetOverlappedResults()
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
// all other error codes
ProcessErrorMessage("WriteFile()");
}
}
}
else
{
LeaveCriticalSection(&m_csCommunicationSync);
}
} // end if(bWrite) if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(m_hComm, // Handle to COMM port
&m_ov, // Overlapped structure
&BytesSent, // Stores number of bytes sent
TRUE); // Wait flag LeaveCriticalSection(&m_csCommunicationSync); // deal with the error code
if (!bResult)
{
ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
}
} // end if (!bWrite) // Verify that the data size send equals what we tried to send
if (BytesSent != strlen((char*)m_szWriteBuffer))
{
TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)m_szWriteBuffer));
}
}
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
unsigned char RXBuff; for (;;)
{
EnterCriticalSection(&m_csCommunicationSync);
bResult = ClearCommError(m_hComm, &dwError, &comstat); LeaveCriticalSection(&m_csCommunicationSync);
if (comstat.cbInQue == 0)
{
// break out when all bytes have been read
break;
}
EnterCriticalSection(&m_csCommunicationSync); if (bRead)
{
bResult = ReadFile(m_hComm, // Handle to COMM port
&RXBuff, // RX Buffer Pointer
1, // Read one byte
&BytesRead, // Stores number of bytes read
&m_ov); // pointer to the m_ov structure
// deal with the error code
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress
// Proceed on to GetOverlappedResults();
bRead = FALSE;
break;
}
default:
{
// Another error has occured. Process this error.
ProcessErrorMessage("ReadFile()");
break;
}
}
}
else
{
// ReadFile() returned complete. It is not necessary to call GetOverlappedResults()
bRead = TRUE;
}
} // close if (bRead) if (!bRead)
{
bRead = TRUE;
bResult = GetOverlappedResult(m_hComm, // Handle to COMM port
&m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
TRUE); // Wait flag // deal with the error code
if (!bResult)
{
ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
}
} // close if (!bRead)
LeaveCriticalSection(&m_csCommunicationSync); // notify parent that a byte was received
::SendMessage(m_pOwner->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) m_nPortNr);
} // end forever loop}void ProcessErrorMessage(char* ErrorText)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); char *Temp = new char[200];
LPVOID lpMsgBuf; FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
); sprintf(Temp, "WARNING: %s Failed with the following error: \n%s\nPort: %d\n", (char*)ErrorText, lpMsgBuf, m_nPortNr);
MessageBox(NULL, Temp, "Application Error", MB_ICONSTOP); LocalFree(lpMsgBuf);
delete[] Temp;
}BOOL InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message)
UINT portnr, // portnumber (1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize) // size to the writebuffer
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); assert(portnr > 0 && portnr < 5);
assert(pPortOwner != NULL); // if the thread is alive: Kill
if (m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE("Thread ended\n");
} // create events
if (m_ov.hEvent != NULL)
ResetEvent(m_ov.hEvent);
m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (m_hWriteEvent != NULL)
ResetEvent(m_hWriteEvent);
m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // initialize the event objects
m_hEventArray[0] = m_hShutdownEvent; // highest priority
m_hEventArray[1] = m_ov.hEvent;
m_hEventArray[2] = m_hWriteEvent; // initialize critical section
InitializeCriticalSection(&m_csCommunicationSync);
// set buffersize for writing and save the owner
m_pOwner = pPortOwner; if (m_szWriteBuffer != NULL)
delete [] m_szWriteBuffer;
m_szWriteBuffer = new char[writebuffersize]; m_nPortNr = portnr; m_nWriteBufferSize = writebuffersize;
m_dwCommEvents = dwCommEvents; BOOL bResult = FALSE;
char *szPort = new char[50];
char *szBaud = new char[50]; // now it critical!
EnterCriticalSection(&m_csCommunicationSync); // if the port is already opened: close it
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
} // prepare port strings
sprintf(szPort, "COM%d", portnr);
sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits); // get a handle to the port
m_hComm = CreateFile(szPort, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices if (m_hComm == INVALID_HANDLE_VALUE)
{
// port not found
delete [] szPort;
delete [] szBaud; return FALSE;
} // set the timeout values
m_CommTimeouts.ReadIntervalTimeout = 1000;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeoutConstant = 1000; // configure
if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if (SetCommMask(m_hComm, dwCommEvents))
{
if (GetCommState(m_hComm, &m_dcb))
{
m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!
if (BuildCommDCB(szBaud, &m_dcb))
{
if (SetCommState(m_hComm, &m_dcb))
; // normal operation... continue
else
ProcessErrorMessage("SetCommState()");
}
else
ProcessErrorMessage("BuildCommDCB()");
}
else
ProcessErrorMessage("GetCommState()");
}
else
ProcessErrorMessage("SetCommMask()");
}
else
ProcessErrorMessage("SetCommTimeouts()"); delete [] szPort;
delete [] szBaud; // flush the port
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); // release critical section
LeaveCriticalSection(&m_csCommunicationSync); TRACE("Initialisation for communicationport %d completed.\nUse Startmonitor to communicate.\n", portnr); return TRUE;
}