目前我正在研究 Adobe AIR Native 扩展,它提供了使用 TWAIN 简单操作图像扫描仪的可能性。
我使用在找到的CTwain类http://www.codeproject.com/Articles/296/AC-Wrapper-for-TWAIN
当我在 Windows 应用程序(.exe)中使用这个类时,它按预期工作,但在 dll 中(我需要创建一个文件)它在关闭 Twain 设备 UI 时崩溃(当扫描完成或单击取消按钮时)
我认为问题出在 DllMain.cpp 文件中的某处(可能是消息循环),因为在具有启动函数APIENTRY _tWinMain的应用程序中它运行良好。
代码
DllMain.cpp
#include "stdafx.h"
#include "TwainCpp.h"
#include "resource.h"
using namespace std;
HWND g_hwnd = NULL;
HINSTANCE g_hInstance = NULL;
BOOL isValid = false;
BOOL isCreated = false;
CTwain *twain = NULL;
LRESULT CALLBACK WndProc(HWND hWnd, unsigned message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
BOOL CreateAppWindow()
{
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = g_hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
wc.lpszMenuName = "FRETwainMenu";
wc.lpszClassName = "FRETwainClass";
if(RegisterClass(&wc))
{
HWND hWnd;
char title[50];
wsprintf(title, "FRETwain:%x", g_hInstance);
hWnd = CreateWindow("FRETwainClass", title, WS_DISABLED, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_MESSAGE, NULL, g_hInstance, NULL);
if(hWnd)
{
g_hwnd = hWnd;
return TRUE;
}
return FALSE;
}
return FALSE;
}
DWORD WINAPI CreateAppThread()
{
if(CreateAppWindow())
{
MSG msg;
isCreated = true;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
if(twain != NULL){
twain->ProcessMessage(msg);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return TRUE;
}
return FALSE;
}
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD reason, LPVOID lpvReserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
g_hInstance = hInstance;
HANDLE thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CreateAppThread, (LPVOID)NULL, 0, NULL);
break;
}
return TRUE;
}
FRETwain.cpp(上下文文件)
#include <windows.h>
#include "FRETwain.h"
extern BOOL isValid;
extern BOOL isCreated;
extern HWND g_hwnd;
extern CTwain *twain;
extern "C"
{
FREObject AcquireTwain(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
{
FREObject result;
uint32_t ret = 0;
if(isValid){
twain->Acquire(TWCPP_ANYCOUNT);
ret = 1;
}
FRENewObjectFromBool(ret, &result);
return result;
}
FREObject setDefaultDevice(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
{
FREObject result;
uint32_t ret = 0;
if(isValid){
twain->SelectSource();
ret = 1;
}
FRENewObjectFromBool(ret, &result);
return result;
}
FREObject initTwain(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
{
FREObject result;
uint32_t isTwain = 0;
if(!isValid){
twain = new CTwain(g_hwnd, ctx);
isValid = twain->IsValidDriver();
}
if(isValid)
isTwain = 1;
FRENewObjectFromBool(isTwain, &result);
return result;
}
}