|
CreateFile ReadFile WriteFile DeviceIoControl (将会产生 IRP 包) Closehandle
#pragma once
#include <afxdisp.h> #include <activscp.h>
class CCodeObject; class CScriptSite;
class CScriptingSupportHelper { public: CScriptingSupportHelper(); ~CScriptingSupportHelper();
BOOL Create(CWnd* pWnd); BOOL RunScript(CString str);
CCodeObject* GetCodeObject() const { return m_pCodeObject; } CScriptSite* GetScriptSite() const { return m_pScriptSite; } IActiveScript* GetActiveScript() const { return m_pActiveScript; }
private: CCodeObject* m_pCodeObject; CScriptSite* m_pScriptSite;
IActiveScript* m_pActiveScript; IActiveScriptParse* m_pActiveScriptParse; };
class CCodeObject : public CCmdTarget { public: CCodeObject(CScriptingSupportHelper* pScripting, CWnd* pWnd); virtual ~CCodeObject();
void Line(long, long, long, long); void Ellipse(long, long, long, long); void DrawText(LPCTSTR msg, long x, long y, long w, long h);
void OnPaint(); void OnMouseClick(long x, long y);
private: CWnd* m_pWnd; CScriptingSupportHelper* m_pScripting; BOOL GetDispatch(OLECHAR* name, COleDispatchDriver& disp, DISPID& dispid);
enum { idLine = 1, idEllipse, idDrawText, };
DECLARE_DISPATCH_MAP() };
class CScriptSite : public IActiveScriptSite { public: CScriptSite(CScriptingSupportHelper* pScripting) { m_pScripting = pScripting; }; ~CScriptSite() { };
virtual ULONG STDMETHODCALLTYPE AddRef() { return InterlockedIncrement(&m_nRefCount); } virtual ULONG STDMETHODCALLTYPE Release() { return InterlockedDecrement(&m_nRefCount); }; virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppObj) { *ppObj = NULL;
if ((iid == IID_IUnknown) || (iid == IID_IActiveScriptSite)) { *ppObj= (IActiveScriptSite*)this; AddRef(); return S_OK; }
return E_NOINTERFACE; }
virtual HRESULT STDMETHODCALLTYPE GetLCID(LCID __RPC_FAR *) { return E_NOTIMPL; } virtual HRESULT STDMETHODCALLTYPE GetItemInfo(LPCOLESTR, DWORD, IUnknown __RPC_FAR *__RPC_FAR * pObj, ITypeInfo __RPC_FAR *__RPC_FAR *) { ASSERT(m_pScripting); ASSERT(m_pScripting->GetCodeObject());
*pObj = m_pScripting->GetCodeObject()->GetIDispatch(TRUE); return S_OK; } virtual HRESULT STDMETHODCALLTYPE GetDocVersionString(BSTR __RPC_FAR *) { return E_NOTIMPL; } virtual HRESULT STDMETHODCALLTYPE OnScriptTerminate(const VARIANT __RPC_FAR * ,const EXCEPINFO __RPC_FAR *) { return E_NOTIMPL; }
virtual HRESULT STDMETHODCALLTYPE OnStateChange(SCRIPTSTATE) { return E_NOTIMPL; } virtual HRESULT STDMETHODCALLTYPE OnScriptError(IActiveScriptError __RPC_FAR * pScriptError) { return E_NOTIMPL; } virtual HRESULT STDMETHODCALLTYPE OnEnterScript() { return E_NOTIMPL; } virtual HRESULT STDMETHODCALLTYPE OnLeaveScript() { return E_NOTIMPL; }
private: long m_nRefCount; CScriptingSupportHelper* m_pScripting; };
#include "StdAfx.h" #include "ScriptingSupport.h"
CCodeObject::CCodeObject(CScriptingSupportHelper* pScripting, CWnd* pWnd) : m_pWnd(pWnd), m_pScripting(pScripting)
{ EnableAutomation(); }
CCodeObject::~CCodeObject() { }
BEGIN_DISPATCH_MAP(CCodeObject, CCmdTarget) DISP_FUNCTION_ID(CCodeObject, "Line", idLine, Line, VT_EMPTY, VTS_I4 VTS_I4 VTS_I4 VTS_I4) DISP_FUNCTION_ID(CCodeObject, "Ellipse", idEllipse, Ellipse, VT_EMPTY, VTS_I4 VTS_I4 VTS_I4 VTS_I4) DISP_FUNCTION_ID(CCodeObject, "DrawText", idDrawText, DrawText, VT_EMPTY, VTS_BSTR VTS_I4 VTS_I4 VTS_I4 VTS_I4) END_DISPATCH_MAP()
void CCodeObject::Line(long x1, long y1, long x2, long y2) { CWindowDC dc(m_pWnd);
dc.MoveTo(x1, y1); dc.LineTo(x2, y2); }
void CCodeObject::Ellipse(long x1, long y1, long x2, long y2) { CWindowDC dc(m_pWnd); dc.Ellipse(x1, y1, x2, y2); }
void CCodeObject::DrawText(LPCTSTR msg, long x, long y, long w, long h) { CWindowDC dc(m_pWnd); CRect rect(x, y, x+w, y+h);
dc.DrawText(msg, rect, 0); }
void CCodeObject::OnPaint() { COleDispatchDriver disp; DISPID dispid; if (GetDispatch(L"OnPaint", disp, dispid)) { disp.InvokeHelper(dispid, DISPATCH_METHOD, VT_EMPTY, 0, 0); } }
BOOL CCodeObject::GetDispatch(OLECHAR* name, COleDispatchDriver& disp, DISPID& dispid) { IDispatch* pScriptDispatch = 0; m_pScripting->GetActiveScript()->GetScriptDispatch(0, &pScriptDispatch); disp.AttachDispatch(pScriptDispatch); HRESULT hr = pScriptDispatch->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_SYSTEM_DEFAULT, &dispid); return SUCCEEDED(hr); }
void CCodeObject::OnMouseClick(long x, long y) { COleDispatchDriver disp; DISPID dispid; if (GetDispatch(L"OnMouseClick", disp, dispid)) { disp.InvokeHelper(dispid, DISPATCH_METHOD, VT_EMPTY, 0, (const BYTE*)(VTS_I4 VTS_I4), x, y); } }
CScriptingSupportHelper::CScriptingSupportHelper() : m_pCodeObject(0), m_pScriptSite(0), m_pActiveScript(0), m_pActiveScriptParse(0) { }
CScriptingSupportHelper::~CScriptingSupportHelper() { if (m_pActiveScript) { m_pActiveScript->Close(); m_pActiveScriptParse->Release(); m_pActiveScript->Release(); }
delete m_pCodeObject; m_pCodeObject=0; delete m_pScriptSite; m_pScriptSite=0; }
BOOL CScriptingSupportHelper::RunScript(CString strText) { EXCEPINFO ei = {0}; BSTR bstrText = strText.AllocSysString(); m_pActiveScriptParse->ParseScriptText(bstrText, NULL, NULL, NULL, 0, 0, 0, NULL, &ei); SysFreeString(bstrText);
m_pActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
return TRUE; }
BOOL CScriptingSupportHelper::Create(CWnd* pWnd) { m_pCodeObject = new CCodeObject(this, pWnd); m_pScriptSite = new CScriptSite(this);
CLSID clsidJScript; CLSIDFromProgID(L"JScript", &clsidJScript); CoCreateInstance(clsidJScript, NULL, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void **)&m_pActiveScript); m_pActiveScript->QueryInterface(IID_IActiveScriptParse, (void**)&m_pActiveScriptParse); m_pActiveScript->SetScriptSite(m_pScriptSite); m_pActiveScript->AddNamedItem(L"Code", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE | SCRIPTITEM_GLOBALMEMBERS); m_pActiveScriptParse->InitNew();
return TRUE; }
Microsoft Speech API 5.3 用oleview 可以产生 idl 文件 再用 midl工具 可以产生 tlb,h,c 存根文件 等.
Using MFC to Automate SAPI
Introduction
The
Microsoft Foundation Classes (MFC) provides an easy and convenient way
to automate calls to SAPI using its Class Wizard to generate wrappers
for the SAPI layer from the SAPI Type Library.
In order to accomplish this, perform the following steps:
- Create a new MFCAppWizard(exe) project in Visual C++.
- Based
on the type of application you are creating, follow the wizard prompts.
In Step 3 of the wizard prompts, (or Step 2 if you are creating a
Dialog Based application) make sure that the Automation check box is
selected under the heading, What other support would you like to
include?
Once the new project is ready, access Class Wizard.
- Click the Automation tab, and then click Add Class and select From a type library in the drop-down list.
- Browse for the sapi.dll file and open it.
- Select the
classes you would like Class Wizard to generate a wrapper for. The
resulting default header and implementation files are sapi.h and
sapi.cpp respectively. The rest of this document assumes that you have
chosen to use these default file names. Click OK.
- You should now be back in the Class Wizard window. Click OK.
After you are done with the above steps, Visual C++ will automatically
add the Class Wizard generated files sapi.cpp and sapi.h to your
project.
Upon viewing the sapi.h file, you should notice that it is nothing
more than an automation wrapper that has been generated for all the
classes you selected. Notice that all the classes inherit from
COleDispatchDriver, hence the dispatch interface needs to be set up.
This only requires a few lines of simple code, after which the wrapper
class can be used just like any other C++ class.
Example
This
example assumes that you chose to generate a wrapper for the
ISpeechVoice class from among any other classes you may have selected.
Using the project created above, include the sapi.h file within a
source file in the project that will make automation calls to SAPI
using the wrapper. In that source file, type the following code.
CLSID CLSID_SpVoice; // class ID for the SAPI SpVoice object LPDISPATCH pDisp; // dispatch interface for the class ISpeechVoice voice; // use the MFC Class Wizard generated wrapper
CLSIDFromProgID(L"SAPI.SpVoice", &CLSID;_SpVoice); voice.CreateDispatch(CLSID_SpVoice); pDisp = voice.m_lpDispatch;
HRESULT hr = pDisp->QueryInterface(CLSID_SpVoice, (void**)&voice;.m_lpDispatch);
if (hr == S_OK) { pDisp->Release(); } else { voice.AttachDispatch(pDisp, TRUE); }
voice.Speak("hello world", 1); // asynchronous call to Speak method of ISpeechVoice interface
If you have been following the steps outlined above
properly, you should hear your computer say "hello world!" That's all
there is to using MFC to make automation calls to SAPI. Currently
however, not all the wrapper classes generated by MFC's Class Wizard
work properly. For instance, the ISpeechLexicon interface does not
work. The work around for this is to implement your own automation
wrapper classes using C++. The steps to do that are beyond the scope of
this document. Of course, you can always use the COM interfaces in C++
and Automation in Visual Basic to ensure that every interface in SAPI
works easily and flawlessly.
void opercom() { ::CoInitializeEx(NULL, COINIT_MULTITHREADED); // {2D8EBDEE-437C-47c9-ABCC-F169E5E781D0}speeddial // {85140985-7A18-4009-B5FB-43268FD154F8}ISpRecognizerLite CLSID CLSID_SpVoice; ::CLSIDFromProgID(L"SpeedDial", &CLSID_SpVoice); LPCLASSFACTORY pClsF; LPUNKNOWN punk; ::CoGetClassObject(CLSID_SpVoice,CLSCTX_INPROC_SERVER,NULL,IID_IClassFactory,(void**)&pClsF); pClsF->CreateInstance(NULL,IID_IUnknown,(void**)&punk); punk->QueryInterface(IID_ISpRecognizerLite,(void**)&非抽象类); ::CoUninitialize(); }
BOOL regcom(LPCWSTR strLib) { //for registration HMODULE hLib = ::LoadLibrary(strLib); if(hLib == 0) { return FALSE; } HRESULT (STDAPICALLTYPE *pDllRegisterServer)(); (FARPROC&)pDllRegisterServer = ::GetProcAddress(hLib, _T("DllRegisterServer")); if(pDllRegisterServer == NULL) { ::FreeLibrary(hLib); return FALSE; } if(FAILED(pDllRegisterServer ())) { ::FreeLibrary(hLib); return FALSE; } else { ::FreeLibrary(hLib); return TRUE; } }
BOOL unregcom(LPCWSTR strLib) { HMODULE hLib = ::LoadLibrary(strLib); if(hLib == 0) { return FALSE; } HRESULT (STDAPICALLTYPE *pDllUnregisterServer)(); (FARPROC&)pDllUnregisterServer = ::GetProcAddress(hLib, _T("DllUnregisterServer")); if(pDllUnregisterServer == NULL) { ::FreeLibrary(hLib); return FALSE; } if(FAILED(pDllUnregisterServer())) { ::FreeLibrary(hLib); return FALSE; } else { ::FreeLibrary(hLib); return TRUE; } }
void CopyFilePCtoWinCE(CString strFileNamePC, CString strFileNamePPC) { CFile oldFile; oldFile.Open(strFileNamePC, CFile::modeRead |CFile::typeBinary); int iLen = oldFile.GetLength(); iLen = iLen / BUFFER_SIZE; BSTR bstr = strFileNamePPC.AllocSysString(); SysFreeString(bstr); CeRapiInit(); HANDLE h = CeCreateFile(bstr, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); char cTemp[BUFFER_SIZE]; DWORD nbytes; int iTotBytes = 0; int iReaded=0; while((iReaded=oldFile.Read(&cTemp, BUFFER_SIZE)) >= 1) CeWriteFile(h, &cTemp, (DWORD)iReaded, &nbytes, NULL); CeCloseHandle(h); oldFile.Close(); CeRapiUninit(); }
void CopyFileWinCEtoPC(CString strFileNamePPC, CString strFileNamePC) { BSTR bstr = strFileNamePPC.AllocSysString(); SysFreeString(bstr); CeRapiInit();
HANDLE h; h = CeCreateFile(bstr, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
CFile oldFile; oldFile.Open(strFileNamePC, CFile::modeCreate | CFile::modeWrite);
char cTemp[BUFFER_SIZE]; DWORD nbytes; CString s;
while(CeReadFile(h, &cTemp, (DWORD)BUFFER_SIZE, &nbytes, NULL) == TRUE) { oldFile.Write(&cTemp, nbytes); if(nbytes < BUFFER_SIZE) break; } CeCloseHandle(h); oldFile.Close(); CeRapiUninit(); }
BOOL DeleteFileFromCE(CString strFileNamePPC) { BSTR bstr = strFileNamePPC.AllocSysString(); SysFreeString(bstr); CeRapiInit(); BOOL bRet = CeDeleteFile(bstr); CeRapiUninit(); return bRet; }
bool HaveNOASCII(LPWSTR str) { char nstring[100]={0}; wcstombs( nstring,str,100); return (strlen(nstring)==wcslen(str)); }
http://www.geekpage.jp/en/programming/directshow/getcurrentimage.php
#include <stdio.h>
#include <dshow.h>
// change here
#define FILENAME L"C:\\DXSDK\\Samples\\Media\\butterfly.mpg"
// note that this sample fails on some environment
int
main()
{
IGraphBuilder *pGraphBuilder;
IMediaControl *pMediaControl;
IBasicVideo *pBasicVideo;
CoInitialize(NULL);
CoCreateInstance(CLSID_FilterGraph,
NULL,
CLSCTX_INPROC,
IID_IGraphBuilder,
(LPVOID *)&pGraphBuilder);
pGraphBuilder->QueryInterface(IID_IMediaControl,
(LPVOID *)&pMediaControl);
pMediaControl->RenderFile(FILENAME);
pGraphBuilder->QueryInterface(IID_IBasicVideo,
(LPVOID *)&pBasicVideo);
pMediaControl->Run();
// The image will be saved when OK is clicked
MessageBox(NULL,
"Grab Image",
"Grab",
MB_OK);
// Must Pause before using GetCurrentImage
pMediaControl->Pause();
// get width and height
long height, width;
pBasicVideo->get_VideoHeight(&height);
pBasicVideo->get_VideoWidth(&width);
long bufSize;
long *imgData;
HRESULT hr;
/*
The second value is NULL to resolve required buffer size.
The required buffer size will be returned in variable "bufSize".
*/
hr = pBasicVideo->GetCurrentImage(&bufSize, NULL);
if (FAILED(hr)) {
printf("GetCurrentImage failed\n");
return 1;
}
if (bufSize < 1) {
printf("failed to get data size\n");
return 1;
}
imgData = (long *)malloc(bufSize);
// The data will be in DIB format
pBasicVideo->GetCurrentImage(&bufSize, imgData);
// save DIB file as Bitmap.
// This sample saves image as bitmap to help
// understanding the sample.
HANDLE fh;
BITMAPFILEHEADER bmphdr;
BITMAPINFOHEADER bmpinfo;
DWORD nWritten;
memset(&bmphdr, 0, sizeof(bmphdr));
memset(&bmpinfo, 0, sizeof(bmpinfo));
bmphdr.bfType = ('M' << 8) | 'B';
bmphdr.bfSize = sizeof(bmphdr) + sizeof(bmpinfo) + bufSize;
bmphdr.bfOffBits = sizeof(bmphdr) + sizeof(bmpinfo);
bmpinfo.biSize = sizeof(bmpinfo);
bmpinfo.biWidth = width;
bmpinfo.biHeight = height;
bmpinfo.biPlanes = 1;
bmpinfo.biBitCount = 32;
fh = CreateFile("result.bmp",
GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(fh, &bmphdr, sizeof(bmphdr), &nWritten, NULL);
WriteFile(fh, &bmpinfo, sizeof(bmpinfo), &nWritten, NULL);
WriteFile(fh, imgData, bufSize, &nWritten, NULL);
CloseHandle(fh);
free(imgData);
// Release resource
pBasicVideo->Release();
pMediaControl->Release();
pGraphBuilder->Release();
CoUninitialize();
return 0;
}
You can use functions that are exposed by the WDbgExts_CE.h header file in debugger extension commands. When developing a debugger extension, these functions can be helpful in controlling and examining the target device being debugged.
The following table shows debugger extension functions.
Programming element |
Description |
CheckControlC
|
This function checks to see whether the user pressed the CTRL+C key combination.
|
Disassm
|
This function disassembles an instruction and stores in a buffer a string that can be printed.
|
dprintf
|
This function prints a formatted string to the command window for the debugger.
|
EXTSTACKTRACE
|
This structure specifies stack frames for the StackTrace function.
|
GetContext
|
This function obtains the context of the process being debugged.
|
GetDebuggerData
|
This function retrieves information stored in a data block.
|
GetExpression
|
This function returns the value of an expression.
|
GetSetSympath
|
This function obtains or sets the search path for symbols.
|
GetSymbol
|
This function locates the symbol nearest to a specified address.
|
Ioctl
|
This function is an entry point for much of the functionality provided by the extension functions for the kernel debugger.
|
ReadControlSpace
|
This function reads a CPU-specific control space into an array.
|
ReadMemory
|
This function reads memory from the process being debugged.
The entire area of memory must be accessible, or the operation fails.
|
ReadPhysical
|
This function reads from physical memory.
|
SetContext
|
This function sets the context of the process being debugged.
|
SetThreadForOperation
|
This function specifies a thread to use for the next call to the StackTrace function
|
StackTrace
|
This function receives a stack trace for the process being debugged.
|
WriteIoSpace
|
This function writes to system I/O locations.
|
WriteMemory
|
This function writes memory to a process being debugged.
The entire area of memory must be accessible, or the operation fails.
|
WritePhysical
|
This function writes to physical memory.
|
|