Need help getting HWND for SendMessage() in MFC ActiveX user control

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I am writing an MFC ActiveX control for Windows CE. The control
contains a simply Edit box, and has a few simple properties. It
contains the following:
1. m_MainDialog - A member variable for the main dialog of the
control
2. m_EditBox - A member variable corresponding to the Edit box
contained in the main dialog
3. m_value - The text value of m_EditBox
4. Clear() - A method used to clear the contents of the Edit box
5. OnValueChanged() - A method to handle updating the m_value
variable
6. FireTextChanged() - An even that is fired whenever the m_value
is changed
7. SetText() - A method used to update the m_value variable

The user control accesses a built-in scanner's API, and must place a
scanned barcode's value into the Edit box, while updating the value of
m_value. Upon scanning a valid barcode, the application should also
fire the TextChanged() event, so it can be handled externally (i.e.
javascript).

I have most of the code working properly... but I am having some
troubles completing my ScanThread function. When a user scans a
barcode, the ScanThread function should play a sound, and
SendMessage(WM_SCAN, ...) back to the handling WndProc, which is
contained in the same class. I am pretty sure I'm not initializing my
HWND hDlg variable correctly, but have not been able to find much help
on the subject (since this is a user control, not an application).

My source code for the class in question is below, but as I said, I
think I just need to get SendMessage working properly in my ScanThread
function. The call to SendMessage should send WM_SCAN back to the
handling WndProc. Search for the following to find this section in
code:
"// My tries on sending the WM_SCAN message to the WndProc procedure
above"






/
********************************************************************************
*********** BarcodeBoxCtrl.h
********************************************************************************/
#if !
defined(AFX_BARCODEBOXCTL_H__B0DA19C0_6986_4621_A26E_911B511CC1A4__INCLUDED_)
#define
AFX_BARCODEBOXCTL_H__B0DA19C0_6986_4621_A26E_911B511CC1A4__INCLUDED_

#include "MainDialog.h" // Added by ClassView
#include <ObjSafe.h>

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

// BarcodeBoxCtl.h : Declaration of the CBarcodeBoxCtrl ActiveX
Control class.

/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl : See BarcodeBoxCtl.cpp for implementation.

class CBarcodeBoxCtrl : public COleControl
{
DECLARE_DYNCREATE(CBarcodeBoxCtrl)

// Constructor
public:
CBarcodeBoxCtrl();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBarcodeBoxCtrl)
public:
virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect&
rcInvalid);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void DoPropExchange(CPropExchange* pPX);
virtual void OnResetState();
virtual DWORD GetControlFlags();
protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM
lParam);
//}}AFX_VIRTUAL

// Implementation
protected:
~CBarcodeBoxCtrl();

void SetText(CString buffer);

DECLARE_OLECREATE_EX(CBarcodeBoxCtrl) // Class factory and guid
DECLARE_OLETYPELIB(CBarcodeBoxCtrl) // GetTypeInfo
DECLARE_PROPPAGEIDS(CBarcodeBoxCtrl) // Property page IDs
DECLARE_OLECTLTYPE(CBarcodeBoxCtrl) // Type name and misc status

/****************
** The code below this line is used to mark this control as SAFE for
scripting.
***************/

DECLARE_INTERFACE_MAP()
BEGIN_INTERFACE_PART(ObjSafe, IObjectSafety)
STDMETHOD_(HRESULT, GetInterfaceSafetyOptions) (
/* [in] */ REFIID riid,
/* [out] */ DWORD __RPC_FAR *pdwSupportedOptions,
/* [out] */ DWORD __RPC_FAR *pdwEnabledOptions
);

STDMETHOD_(HRESULT, SetInterfaceSafetyOptions) (
/* [in] */ REFIID riid,
/* [in] */ DWORD dwOptionSetMask,
/* [in] */ DWORD dwEnabledOptions
);
END_INTERFACE_PART(ObjSafe);

/***************/

// Subclassed control support
BOOL IsSubclassedControl();
LRESULT OnOcmCommand(WPARAM wParam, LPARAM lParam);
//LRESULT OnScan(WPARAM wParam, LPARAM lParam);

// Message maps
//{{AFX_MSG(CBarcodeBoxCtrl)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()

// Dispatch maps
//{{AFX_DISPATCH(CBarcodeBoxCtrl)
CString m_value;
afx_msg void OnValueChanged();
afx_msg void Clear();
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()

// Event maps
//{{AFX_EVENT(CBarcodeBoxCtrl)
void FireTextChanged()
{FireEvent(eventidTextChanged,EVENT_PARAM(VTS_NONE));}
//}}AFX_EVENT
DECLARE_EVENT_MAP()

// Dispatch and event IDs
public:
CMainDialog m_MainDialog;
enum {
//{{AFX_DISP_ID(CBarcodeBoxCtrl)
dispidValue = 1L,
dispidClear = 2L,
eventidTextChanged = 1L,
//}}AFX_DISP_ID
};
};

//{{AFX_INSERT_LOCATION}}
// Microsoft eMbedded Visual C++ will insert additional declarations
immediately before the previous line.

#endif // !
defined(AFX_BARCODEBOXCTL_H__B0DA19C0_6986_4621_A26E_911B511CC1A4__INCLUDED)







/
********************************************************************************
*********** BarcodeBoxCtrl.cpp
********************************************************************************/
// BarcodeBoxCtl.cpp : Implementation of the CBarcodeBoxCtrl ActiveX
Control class.

#include "stdafx.h"
#include "BarcodeBox.h"
#include "BarcodeBoxCtl.h"
#include "BarcodeBoxPpg.h"
#include <commctrl.h> // Added for scanner implementation
#include "FJAPI.h" // Added for scanner implementation

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$*/

//CBarcodeBoxCtrl MessageHandler; // User this for
SendMessage(WM_SCAN, ...) ?

void WriteString(CString str);
CString buffer = ""; // Used as a temporary fix to sending the
WM_SCAN message

HWND hWnd;

//Scanner Sound
#define WAVE_SOUND _T("\\Windows\\Asterisk.wav")
#define SCAN_WAVE_SOUND _T("\\Flashdisk\\scanbeep.wav")
#define HONK_WAVE_SOUND _T("\\Flashdisk\\honk.wav")
#define ERROR_WAVE_SOUND _T("\\Flashdisk\\Error.wav")

#define WM_SCAN WM_APP+18
#define WM_APP_RESUME WM_APP+32

//Error Messageg
#define THREAD_ERR_MSG _T("CreateThread Error")
#define SCN_CAPTION _T("Scanner Test")

//Global Variables
HINSTANCE hInst = NULL;
HANDLE m_hCSEventThread;
HWND hWndControls[1]; // control handles

HANDLE hResumeThread = NULL;
HANDLE hStopScannerEvent = NULL;
HANDLE hScannerThread = NULL;
//HANDLE hShutdownEvent = NULL;
//HANDLE hThread = NULL;

//Functions
//DWORD ScanThread(LPVOID lpvParam);
long WINAPI ResumeEventThread (LPVOID pObj);
void KillScannerThread();
long WINAPI ScanThread (LPVOID pObj);

extern DWORD TP_Scan_Load(void);
extern DWORD TP_Scan_Unload(void);
extern DWORD TP_Scan_Open(void);
extern DWORD TP_Scan_Close(void);
extern DWORD TP_Scan_Read(unsigned char *uBuf,
DWORD *dwSize);
DWORD dwThreadID;

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$*/


#define afxRegBothThreading 0x0004

IMPLEMENT_DYNCREATE(CBarcodeBoxCtrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CBarcodeBoxCtrl, COleControl)
//{{AFX_MSG_MAP(CBarcodeBoxCtrl)
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_MESSAGE(OCM_COMMAND, OnOcmCommand)
//ON_MESSAGE(WM_SCAN, OnScan)
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CBarcodeBoxCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CBarcodeBoxCtrl)
DISP_PROPERTY_NOTIFY(CBarcodeBoxCtrl, "Value", m_value,
OnValueChanged, VT_BSTR)
DISP_FUNCTION(CBarcodeBoxCtrl, "Clear", Clear, VT_EMPTY, VTS_NONE)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CBarcodeBoxCtrl, COleControl)
//{{AFX_EVENT_MAP(CBarcodeBoxCtrl)
EVENT_CUSTOM("TextChanged", FireTextChanged, VTS_NONE)
//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed. Remember to increase the
count!
BEGIN_PROPPAGEIDS(CBarcodeBoxCtrl, 1)
PROPPAGEID(CBarcodeBoxPropPage::guid)
END_PROPPAGEIDS(CBarcodeBoxCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CBarcodeBoxCtrl, "BARCODEBOX.BarcodeBoxCtrl.1",
0x96ce5bf7, 0x96e8, 0x4107, 0xab, 0x5b, 0x9d, 0x14, 0xca, 0x9d, 0x2a,
0xb7)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CBarcodeBoxCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DBarcodeBox =
{ 0xa5d8670f, 0xac16, 0x4833, { 0xaa, 0x36, 0xba, 0x8, 0x36, 0x4c,
0xe3, 0xe0 } };
const IID BASED_CODE IID_DBarcodeBoxEvents =
{ 0x9cd0af7e, 0xd01d, 0x4e4e, { 0x9e, 0xb3, 0xc5, 0xa7, 0x1, 0x90,
0xac, 0x18 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwBarcodeBoxOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CBarcodeBoxCtrl, IDS_BARCODEBOX,
_dwBarcodeBoxOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::CBarcodeBoxCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CBarcodeBoxCtrl

BOOL CBarcodeBoxCtrl::CBarcodeBoxCtrlFactory::UpdateRegistry(BOOL
bRegister)
{
// TODO: Verify that your control follows apartment-model threading
rules.
// Refer to MFC TechNote 64 for more information.
// If your control does not conform to the apartment-model rules,
then
// you must modify the code below, changing the 6th parameter from
// afxRegApartmentThreading to 0.

if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_BARCODEBOX,
IDB_BARCODEBOX,
afxRegBothThreading,
_dwBarcodeBoxOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::CBarcodeBoxCtrl - Constructor

CBarcodeBoxCtrl::CBarcodeBoxCtrl()
{
InitializeIIDs(&IID_DBarcodeBox, &IID_DBarcodeBoxEvents);

// TODO: Initialize your control's instance data here

}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::~CBarcodeBoxCtrl - Destructor

CBarcodeBoxCtrl::~CBarcodeBoxCtrl()
{
// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::OnDraw - Drawing function

void CBarcodeBoxCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const
CRect& rcInvalid)
{
DoSuperclassPaint(pdc, rcBounds);
m_MainDialog.MoveWindow(rcBounds, TRUE);
CBrush brBackGnd(TranslateColor(AmbientBackColor()));
pdc->FillRect(rcBounds, &brBackGnd);
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::DoPropExchange - Persistence support

void CBarcodeBoxCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);

// TODO: Call PX_ functions for each persistent custom property.
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::GetControlFlags -
// Flags to customize MFC's implementation of ActiveX controls.
//
// For information on using these flags, please see MFC technical note
// #nnn, "Optimizing an ActiveX Control".
DWORD CBarcodeBoxCtrl::GetControlFlags()
{
DWORD dwFlags = COleControl::GetControlFlags();

// The control will not be redrawn when making the transition
// between the active and inactivate state.
dwFlags |= noFlickerActivate;
return dwFlags;
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::OnResetState - Reset control to default state

void CBarcodeBoxCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in
DoPropExchange

// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::PreCreateWindow - Modify parameters for
CreateWindowEx

BOOL CBarcodeBoxCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
cs.lpszClass = _T("EDIT");
return COleControl::PreCreateWindow(cs);
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::IsSubclassedControl - This is a subclassed control

BOOL CBarcodeBoxCtrl::IsSubclassedControl()
{
return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::OnOcmCommand - Handle command messages

LRESULT CBarcodeBoxCtrl::OnOcmCommand(WPARAM wParam, LPARAM lParam)
{
#ifdef _WIN32
WORD wNotifyCode = HIWORD(wParam);
#else
WORD wNotifyCode = HIWORD(lParam);
#endif

// TODO: Switch on wNotifyCode here.

return 0;
}


/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl message handlers

int CBarcodeBoxCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
m_MainDialog.Create(IDD_MAINDIALOG, this);

return 0;
}

/****************
** The code below this line is used to mark this control as SAFE for
scripting.
***************/

const DWORD dwSupportedBits =
INTERFACESAFE_FOR_UNTRUSTED_CALLER |
INTERFACESAFE_FOR_UNTRUSTED_DATA;
const DWORD dwNotSupportedBits = ~ dwSupportedBits;

/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::XObjSafe::GetInterfaceSafetyOptions
// Allows container to query what interfaces are safe for what. We're
// optimizing significantly by ignoring which interface the caller is
// asking for.
HRESULT STDMETHODCALLTYPE
CBarcodeBoxCtrl::XObjSafe::GetInterfaceSafetyOptions(
/* [in] */ REFIID riid,
/* [out] */ DWORD __RPC_FAR *pdwSupportedOptions,
/* [out] */ DWORD __RPC_FAR *pdwEnabledOptions)
{
METHOD_PROLOGUE(CBarcodeBoxCtrl, ObjSafe)

HRESULT retval = ResultFromScode(S_OK);

// Does interface exist?
IUnknown FAR* punkInterface;
retval = pThis->ExternalQueryInterface(&riid,
(void * *)&punkInterface);
if (retval != E_NOINTERFACE) { // interface exists
punkInterface->Release(); // release it--just checking!
}

// We support both kinds of safety and have always both set,
// regardless of interface.
*pdwSupportedOptions = *pdwEnabledOptions = dwSupportedBits;
return retval; // E_NOINTERFACE if QI failed
}

/////////////////////////////////////////////////////////////////////////////
// CBarcodeBoxCtrl::XObjSafe::SetInterfaceSafetyOptions
// Since we're always safe, this is a no-brainer--but we do check to
make
// sure the interface requested exists and that the options we're
asked to
// set exist and are set on (we don't support unsafe mode).
HRESULT STDMETHODCALLTYPE
CBarcodeBoxCtrl::XObjSafe::SetInterfaceSafetyOptions(
/* [in] */ REFIID riid,
/* [in] */ DWORD dwOptionSetMask,
/* [in] */ DWORD dwEnabledOptions)
{
METHOD_PROLOGUE(CBarcodeBoxCtrl, ObjSafe)

// Does interface exist?
IUnknown FAR* punkInterface;
pThis->ExternalQueryInterface(&riid, (void * *)&punkInterface);
if (punkInterface) { // interface exists
punkInterface->Release(); // release it--just checking!
}
else { // Interface doesn't exist.
return ResultFromScode(E_NOINTERFACE);
}
// Can't set bits we don't support.
if (dwOptionSetMask & dwNotSupportedBits) {
return ResultFromScode(E_FAIL);
}

// Can't set bits we do support to zero
dwEnabledOptions &= dwSupportedBits;
// (We already know there are no extra bits in mask. )
if ((dwOptionSetMask & dwEnabledOptions) !=
dwOptionSetMask) {
return ResultFromScode(E_FAIL);
}

// Don't need to change anything since we're always safe.
return ResultFromScode(S_OK);
}

/////////////////////////////////////////////////////////////////////////////
// Interface map for IObjectSafety
BEGIN_INTERFACE_MAP( CBarcodeBoxCtrl, COleControl )
INTERFACE_PART(CBarcodeBoxCtrl, IID_IObjectSafety, ObjSafe)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// IObjectSafety member functions
// Delegate AddRef, Release, QueryInterface
ULONG FAR EXPORT CBarcodeBoxCtrl::XObjSafe::AddRef()
{
METHOD_PROLOGUE(CBarcodeBoxCtrl, ObjSafe)
return pThis->ExternalAddRef();
}
ULONG FAR EXPORT CBarcodeBoxCtrl::XObjSafe::Release()
{
METHOD_PROLOGUE(CBarcodeBoxCtrl, ObjSafe)
return pThis->ExternalRelease();
}
HRESULT FAR EXPORT CBarcodeBoxCtrl::XObjSafe::QueryInterface(
REFIID iid, void FAR* FAR* ppvObj)
{
METHOD_PROLOGUE(CBarcodeBoxCtrl, ObjSafe)
return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}

void CBarcodeBoxCtrl::OnValueChanged()
{
// TODO: Add notification handler code
// This is where the Barcode will be Barcode will be set upon scan.
// Need to set the value of m_Value to the Barcode that was scanned
m_MainDialog.m_EditBox.SetWindowTextW(m_value); // Update the Textbox
display
FireTextChanged(); // Fire an event notifying the browser that the
text has changed

SetModifiedFlag();
}

void CBarcodeBoxCtrl::Clear()
{
// TODO: Add your dispatch handler code here
m_value = ""; // Clear the value from the Textbox
m_MainDialog.m_EditBox.SetWindowTextW(m_value); // Update the Textbox
display
FireTextChanged(); // Fire an event notifying the browser that the
text has changed
}

void CBarcodeBoxCtrl::SetText(CString buffer)
{
// TODO: Add your dispatch handler code here
m_value = buffer; // Clear the value from the Textbox
m_MainDialog.m_EditBox.SetWindowTextW(m_value); // Update the Textbox
display
FireTextChanged(); // Fire an event notifying the browser that the
text has changed
}


/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$*/

LRESULT CBarcodeBoxCtrl::WindowProc(UINT msg, WPARAM wParam, LPARAM
lParam)
{
// TODO: Add your specialized code here and/or call the base class

HWND hDlg = (HWND)IDS_BARCODEBOX;

//if (buffer != "") msg = WM_SCAN; // if there is something in the
buffer, handle WM_SCAN message (temporary fix to sending WM_SCAN)

switch(msg) {
case WM_CREATE:
// start barcode scanner thread
hStopScannerEvent = CreateEvent(0, TRUE, FALSE, 0);
if (hScannerThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)ScanThread, hDlg, 0, &dwThreadID))
CloseHandle(hScannerThread);

//hStopResumeEvent = CreateEvent(0, TRUE, FALSE, 0);
dwThreadID = 0;
if (hResumeThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)ResumeEventThread, hDlg, 0, &dwThreadID))
CloseHandle(hResumeThread);
break;

case WM_SCAN: // This message is never received!!
SetText(buffer);
buffer = ""; // shouldn't need this once WM_SCAN is properly
received
break;

case WM_CLOSE:
TP_Scan_Close();
TP_Scan_Unload();
CloseHandle(m_hCSEventThread);
ExitThread(1);
break;

default:
/*if (buffer != "")
{
this->SendMessage(WM_SCAN, 0L, 0L); // won't need any of this
either
}*/
break;
}

return COleControl::WindowProc(msg, wParam, lParam);
}

long WINAPI ResumeEventThread(LPVOID Param)
{
HWND hDlg = (HWND)Param;
HANDLE hPowIntr;

hPowIntr = CreateEvent(NULL, TRUE, FALSE, TEXT("ResumeHHT"));
if (hPowIntr)
{
while(1)
{
if (WAIT_OBJECT_0 == WaitForSingleObject(hPowIntr, INFINITE))
PostMessage(hDlg, WM_APP_RESUME, 0L, 0L);

Sleep(10);
}
CloseHandle(hPowIntr);
}
return 0;
}

long WINAPI ScanThread(LPVOID lpvParam)
{
HWND hDlg = (HWND)lpvParam;
DWORD dwReadSize;
char cBuff[512];
TCHAR tcBuff[512];

TP_Scan_Load();
TP_Scan_Open();

while(1)
{
if (WAIT_OBJECT_0 == WaitForSingleObject(hStopScannerEvent,0))
break;

Sleep(10);

dwReadSize = 100;
if(TP_Scan_Read((unsigned char *)cBuff, &dwReadSize) == SCAN_OK)
{
if(dwReadSize != 0)
{
memset(tcBuff,0,sizeof(tcBuff));

if((cBuff[dwReadSize-1] <'\x20') // Printable characters only
|| (cBuff[dwReadSize-1] >'\x7f'))
{
dwReadSize--;
}
cBuff[dwReadSize] = 0;

MultiByteToWideChar(CP_ACP,MB_COMPOSITE,cBuff,dwReadSize,tcBuff,dwReadSize);

PlaySound(SCAN_WAVE_SOUND, NULL, SND_FILENAME | SND_ASYNC);

buffer = cBuff; // Set the value of our buffer variable to store
the Barcode

//CBarcodeBoxCtrl *MessageHandler = new CBarcodeBoxCtrl();
//MessageHandler->SendMessage(WM_SCAN, 0L, 0L);

// My tries on sending the WM_SCAN message to the WndProc
procedure above
::SendDlgItemMessageW(hDlg, IDS_BARCODEBOX, WM_SCAN,
(WPARAM)&tcBuff[0], 0L);
SendMessage((HWND)IDS_BARCODEBOX, WM_SCAN, (WPARAM)&tcBuff[0],
0L);
SendMessage(hDlg, WM_SCAN, (WPARAM)&tcBuff[0], 0L);
//MessageHandler.SendMessage(WM_SCAN, (WPARAM)&tcBuff[0], 0L); //
Might not work because MessageHandler is never initialized?
}
} else {
//SEND ERROR message
}
}

TP_Scan_Close();
TP_Scan_Unload();

return(0);
}

void KillScannerThread()
{
if (hScannerThread != NULL)
{
SetEvent(hStopScannerEvent);
WaitForSingleObject(hScannerThread, INFINITE);
}
}

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$*/

/*LRESULT CBarcodeBoxCtrl::OnScan(WPARAM wParam, LPARAM lParam)
{
//SetText(buffer);
return 0;
}*/

// This method is used for debugging purposes only
void WriteString(CString str)
{
HANDLE hFile;
DWORD wmWritten;

hFile = CreateFile(_T("\\Windows\\Desktop\\debug.txt"), GENERIC_READ|
GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

WriteFile(hFile, str,(DWORD)(sizeof(str)), &wmWritten, NULL);

CloseHandle(hFile);
}


.



Relevant Pages

  • Re: Asp.Net AJAX dynamically added Tab Control not displaying
    ... TabPanel assumes there's no active tab and it hides all the tabs at client ... protected void Page_PreRender ... Microsoft is providing this information as a convenience to you. ... does not control these sites and has not tested any software or information ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How to transparent editbox control works correctly...
    ... can't simply attach a CString to your edit control and hope that UpdateData ... void SetFont; ... afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); ...
    (microsoft.public.vc.mfc)
  • Re: Batch file and MFC (Properly Terminating Application)
    ... // CDummy1Dlg message handlers ... // Set the icon for this dialog. ... void CDummy1Dlg::OnPaint ... Use a control variable and do ...
    (microsoft.public.vc.mfc)
  • TextBox Readonly OnPaint wrong font! Please help!
    ... I have a read-only textBox which shows the results of a selection on ... This works until I left click the control, ... protected override void OnPaint ... private txtBx txtBxWithFontProblem; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Numeric spin control,
    ... LRESULT CMyControl::OnSetText(WPARAM, LPARAM lParam) ... CString s; ... formatting in the spin control, do a GetBuddy call, and set the text. ... double GetScaleFactor() ...
    (microsoft.public.vc.mfc)