将一个可以成功运行的程序包装成DLL,调用就出错了,请高手解答!
whlwh 2008-07-22 03:52:36 I am trying to make an activeX dll with VC9 and atl in order to create
an image that will render an HTML page host by a Ibrowser control.
Actually the code is running but it will do nothing... I don't know
where the problem is. I have done a dll with an interface called
Snapshot which have a method GetURL. By calling this method I want to
specify an url to grab on the fly. I think that the problem occured
when I try to call ::CreateWindow... Please help me to find what I
have to change. Here is my actual code :
//////////////////////////////////////////////////////////////////
// CMain
//////////////////////////////////////////////////////////////////
class CMain :
public CWindowImpl <CMain>
{
friend CEventSink;
public:
CMain(LPTSTR uri, LPTSTR file, UINT delay, BOOL silent) : m_uDelay(delay),
m_dwCookie(0), m_URI(uri), m_fileName(file), m_bSilent(silent)/*,m_ax(NULL)*/ { }
BEGIN_MSG_MAP(CMainWindow)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_TIMER, OnTimer)
END_MSG_MAP()
LRESULT OnCreate (UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnSize (UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnDestroy (UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnTimer (UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
BOOL SaveSnapshot(void);
BOOL DelayedSnapshot(void);
private:
LPTSTR m_URI;
LPTSTR m_fileName;
BOOL m_bSilent;
UINT m_uDelay;
UINT m_nIDEvent;
protected:
CComPtr<IUnknown> m_pWebBrowserUnk;
CComPtr<IWebBrowser2> m_pWebBrowser;
CComObject<CEventSink>* m_pEventSink;
HWND m_hwndWebBrowser;
DWORD m_dwCookie;
};
//////////////////////////////////////////////////////////////////
// Implementation of CMain Messages
//////////////////////////////////////////////////////////////////
LRESULT CMain::OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
HRESULT hr;
RECT old;
IUnknown * pUnk = NULL;
GetClientRect(&old);
m_hwndWebBrowser = ::CreateWindow(_T(ATLAXWIN_CLASS), m_URI,
WS_CHILD|WS_DISABLED, old.top, old.left, old.right,
old.bottom, m_hWnd, NULL, ::GetModuleHandle(NULL), NULL);
// TODO: Quit if m_hwndWebBrowser is null.
hr = AtlAxGetControl(m_hwndWebBrowser, &m_pWebBrowserUnk);
if (FAILED(hr))
return 1;
if (m_pWebBrowserUnk == NULL)
return 1;
hr = m_pWebBrowserUnk->QueryInterface(IID_IWebBrowser2, (void**)&m_pWebBrowser);
if (FAILED(hr))
return 1;
// Set whether it should be silent
m_pWebBrowser->put_Silent(m_bSilent ? VARIANT_TRUE : VARIANT_FALSE);
hr = CComObject<CEventSink>::CreateInstance(&m_pEventSink);
if (FAILED(hr))
return 1;
m_pEventSink->m_pMain = this;
hr = AtlAdvise(m_pWebBrowserUnk, m_pEventSink->GetUnknown(),
DIID_DWebBrowserEvents2, &m_dwCookie);
if (FAILED(hr))
return 1;
return 0;
}
static const GUID myGUID = { 0x445c10c2, 0xa6d4, 0x40a9, { 0x9c, 0x3f, 0x4e, 0x90, 0x42, 0x1d, 0x7e, 0x83 } };
static CComModule _Main;
int DllInit(_TCHAR* url,_TCHAR* out)
{
HRESULT hr = _Main.Init(NULL, ::GetModuleHandle(NULL), &myGUID);
if (FAILED(hr))
return EXIT_FAILURE;
bool test = AtlAxWinInit();
if (!test)
return EXIT_FAILURE;
_TCHAR* argUrl = url;
_TCHAR* argOut = out;
int ax;
int argHelp = 0;
int argSilent = 0;
unsigned int argDelay = 0;
unsigned int argMinWidth = 800;
unsigned int argMaxWait = 90000;
if (argUrl == NULL || argOut == NULL || argHelp) {
IECaptHelp();
return 1;
}
CMain MainWnd(argUrl, argOut, argDelay, argSilent);
RECT rcMain = { 0, 0, argMinWidth, 600 };
MainWnd.Create(NULL, rcMain, _T("IECapt"), WS_POPUP);
// TODO: decide what to do when max-wait and delay conflict.
if (argMaxWait != 0)
MainWnd.SetTimer(ID_TIMEOUTTIMER, argMaxWait);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
_Main.Term();
return 0;
}