Z
zytzjx
Unregistered / Unconfirmed
GUEST, unregistred user!
这是用VC6写的DLL
#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif
// This class is exported from the DllTest.dll
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
DLLTEST_API BOOL InitPort(int iCom);
DLLTEST_API void ClosePort();
#ifdef __cplusplus
}
#endif /* __cplusplus */
#include "stdafx.h"
#include "DllTest.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
HANDLE m_hDevice1;
DLLTEST_API BOOL InitPort(int iCom)
{
m_hDevice1 = ::CreateFile("COM4", //
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
//I preserve the device name if it succeeds.
if (m_hDevice1 != INVALID_HANDLE_VALUE)
{
return TRUE;
}
else
{
return FALSE;
}
}
DLLTEST_API void ClosePort()
{
CloseHandle(m_hDevice1);
}
//这是在delphi中的调用
function InitPort(iCom : Integer):Boolean;
stdcall;
procedure ClosePort;stdcall;
function InitPort;
external 'DllTest.dll' name 'InitPort';
procedure ClosePort;
external 'DllTest.dll' name 'ClosePort';
//以下在Delphi中的调用
procedure TForm1.Button2Click(Sender: TObject);
//var
// handle : THANDLE;
begin
if MSCOmm1.PortOpen then
MSComm1.PortOpen := FALSE;
InitPort(4);
ClosePort;
if not MSComm1.PortOpen then
MSComm1.PortOpen := TRUE;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MSComm1.PortOpen := TRUE;
end;
如果上面的InitPort与ClosePort我用下面的来代替就可以。
{ handle := CreateFile('COM4',
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
CloseHandle(handle);
}
上面的API中也就是做了这两件事。我是把我的那个DLL简单出来。
#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif
// This class is exported from the DllTest.dll
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
DLLTEST_API BOOL InitPort(int iCom);
DLLTEST_API void ClosePort();
#ifdef __cplusplus
}
#endif /* __cplusplus */
#include "stdafx.h"
#include "DllTest.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
HANDLE m_hDevice1;
DLLTEST_API BOOL InitPort(int iCom)
{
m_hDevice1 = ::CreateFile("COM4", //
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
//I preserve the device name if it succeeds.
if (m_hDevice1 != INVALID_HANDLE_VALUE)
{
return TRUE;
}
else
{
return FALSE;
}
}
DLLTEST_API void ClosePort()
{
CloseHandle(m_hDevice1);
}
//这是在delphi中的调用
function InitPort(iCom : Integer):Boolean;
stdcall;
procedure ClosePort;stdcall;
function InitPort;
external 'DllTest.dll' name 'InitPort';
procedure ClosePort;
external 'DllTest.dll' name 'ClosePort';
//以下在Delphi中的调用
procedure TForm1.Button2Click(Sender: TObject);
//var
// handle : THANDLE;
begin
if MSCOmm1.PortOpen then
MSComm1.PortOpen := FALSE;
InitPort(4);
ClosePort;
if not MSComm1.PortOpen then
MSComm1.PortOpen := TRUE;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MSComm1.PortOpen := TRUE;
end;
如果上面的InitPort与ClosePort我用下面的来代替就可以。
{ handle := CreateFile('COM4',
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
CloseHandle(handle);
}
上面的API中也就是做了这两件事。我是把我的那个DLL简单出来。