why not found device obj? - c

I write driver .
Creates an Device Object (name = ICOCTL_1) when the service starts but getlastError function return not_foun_file(code 2)
enter image description here
void CUserAppDlg::OnBnClickedbtncreate(){
deviceHandle = CreateFile(L"\\\\.\\IOCTL_1", GENERIC_ALL, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM, 0);
DWORD data = GetLastError();
TCHAR s[100];
_stprintf_s(s, _T("%X"), data);
MessageBox(s);}

Related

Sending POST request with IWebBrowser2 in plain C

I need to compile a POST request routine using IWebBrowser2 in plain C, the problem is that "the post data specified by PostData is passed as a SAFEARRAY Data Type structure. The VARIANT should be of type VT_ARRAY|VT_UI1 and point to a SAFEARRAY Data Type. The SAFEARRAY Data Type should be of element type VT_UI1, dimension one, and have an element count equal to the number of bytes of post data." as it says in https://learn.microsoft.com/en-us/previous-versions/aa752133(v=vs.85)
When i send the request to my local server it just dont receive the $_POST['name'] nor $_POST['sub'] variables, like i didnt send nothing
INT32
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT32 nShowCmd)
{
HRESULT HResult = 0;
CLSID CLSID_IE;
IWebBrowser2 *pWebBrowser;
VARIANT vEmpty;
VARIANT_BOOL vBusy;
VariantInit(&vEmpty);
CoInitializeEx(0, COINIT_MULTITHREADED);
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
CoCreateInstance(&CLSID_InternetExplorer, 0, CLSCTX_ALL, &IID_IWebBrowser2, &pWebBrowser);
IWebBrowser2_put_Visible(pWebBrowser, VARIANT_FALSE);
/* process the formulary data */
LPSTR pPostData = NULL;
CHAR strFormulary[] = "sub=gt&name=sdfs";
LPSAFEARRAY FormularyArray = SafeArrayCreateVector(VT_UI1, 0, sizeof(strFormulary));
VARIANT PostData = FormularyArray;
SafeArrayAccessData(FormularyArray, (LPVOID*)&pPostData);
memcpy(pPostData, &strFormulary, sizeof(strFormulary));
SafeArrayUnaccessData(FormularyArray);
/* Set the headers data */
VARIANT Headers;
V_VT(&Headers) = VT_BSTR;
V_BSTR(&Headers) = SysAllocString(L"Content-Type: application/x-www-form-urlencodedrn");
/* Set POST request */
BSTR bstrURL = SysAllocString(L"http://192.168.100.44/user.php");
IWebBrowser2_Navigate(pWebBrowser, bstrURL, &vEmpty, &vEmpty, &PostData, &Headers);
// Wait for page to load
do {
LARGE_INTEGER TimeOut;
UINT32 Milliseconds = 2000;
TimeOut.QuadPart = UInt32x32To64( Milliseconds, 1000 );
TimeOut.QuadPart *= -1;
NtDelayExecution(FALSE, &TimeOut);
IWebBrowser2_get_Busy(pWebBrowser, &vBusy);
} while(vBusy);
// Get IDispatch interface
IDispatch* pDispatch;
IWebBrowser2_get_Document(pWebBrowser, &pDispatch);
IHTMLDocument2* pDocument;
IDispatch_QueryInterface(pDispatch, &IID_IHTMLDocument2 , &pDocument);
IHTMLElement* lpBodyElm;
IHTMLDocument2_get_body(pDocument, &lpBodyElm);
IHTMLElement* lpParentElm;
IHTMLElement_get_parentElement(lpBodyElm, &lpParentElm);
// Get Inner HTML content content of the request
BSTR bstrBody;
lpParentElm->lpVtbl->get_innerHTML(lpParentElm, &bstrBody);
wprintf(L"%ls\n\n", bstrBody);
cleanup:
SysFreeString(bstrURL);
IWebBrowser2_Quit(pWebBrowser);
IWebBrowser2_Release(pWebBrowser);
IDispatch_Release(pDispatch);
IHTMLDocument2_Release(pDocument);
IHTMLElement_Release(lpBodyElm);
lpParentElm->lpVtbl->Release(lpParentElm);
CoUninitialize();
RtlExitUserProcess(STATUS_SUCCESS);
}
i solved it this way:
INT32
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT32 nShowCmd)
{
HRESULT HResult = 0;
CLSID CLSID_IE;
IWebBrowser2 *pWebBrowser;
VARIANT vEmpty;
VARIANT_BOOL vBusy;
VariantInit(&vEmpty);
CoInitializeEx(0, COINIT_MULTITHREADED);
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
CoCreateInstance(&CLSID_InternetExplorer, 0, CLSCTX_ALL, &IID_IWebBrowser2, &pWebBrowser);
IWebBrowser2_put_Visible(pWebBrowser, VARIANT_FALSE);
/* Set the POST data */
VARIANT PostData;
PostData.vt = VT_NULL;
CHAR PostDataContent[] = "sub=gt&name=sdfs";
UINT32 PostDatalenght = sizeof(PostDataContent);
// Create safe array
SAFEARRAY* psa = SafeArrayCreateVector(VT_UI1, 0, PostDatalenght);
if (psa != NULL)
{
// Store elements into the SafeArray
BYTE HUGEP* pByte = (PBYTE)&PostDataContent;
HRESULT hr = S_OK;
for (long i=0; i<PostDatalenght; i++){
hr = SafeArrayPutElement(psa, &i, pByte);
pByte++;
}
// Get a pointer to the elements of the array.
SafeArrayAccessData(psa, (void HUGEP**)&pByte);
PostData.vt = VT_ARRAY|VT_UI1;
PostData.parray = psa;
}
/* Set the headers data */
VARIANT Headers;
V_VT(&Headers) = VT_BSTR;
V_BSTR(&Headers) = SysAllocString(L"Content-Type: application/x-www-form-urlencoded");
/* Set POST request */
BSTR bstrURL = SysAllocString(L"http://192.168.100.44/user.php");
IWebBrowser2_Navigate(pWebBrowser, bstrURL, &vEmpty, &vEmpty, &PostData, &Headers);
// Wait for page to load
do {
LARGE_INTEGER TimeOut;
UINT32 Milliseconds = 2000;
TimeOut.QuadPart = UInt32x32To64( Milliseconds, 1000 );
TimeOut.QuadPart *= -1;
NtDelayExecution(FALSE, &TimeOut);
IWebBrowser2_get_Busy(pWebBrowser, &vBusy);
} while(vBusy);
// Get IDispatch interface
IDispatch* pDispatch;
IWebBrowser2_get_Document(pWebBrowser, &pDispatch);
IHTMLDocument2* pDocument;
IDispatch_QueryInterface(pDispatch, &IID_IHTMLDocument2 , &pDocument);
IHTMLElement* lpBodyElm;
IHTMLDocument2_get_body(pDocument, &lpBodyElm);
IHTMLElement* lpParentElm;
IHTMLElement_get_parentElement(lpBodyElm, &lpParentElm);
// Get Inner HTML content content of the request
BSTR bstrBody;
lpParentElm->lpVtbl->get_innerHTML(lpParentElm, &bstrBody);
wprintf(L"%ls\n\n", bstrBody);
cleanup:
SysFreeString(bstrURL);
IWebBrowser2_Quit(pWebBrowser);
IWebBrowser2_Release(pWebBrowser);
IDispatch_Release(pDispatch);
IHTMLDocument2_Release(pDocument);
IHTMLElement_Release(lpBodyElm);
lpParentElm->lpVtbl->Release(lpParentElm);
CoUninitialize();
RtlExitUserProcess(STATUS_SUCCESS);
}

Read .txt file using FltReadFile() from minifilter

I am trying to read a config.txt file that contains a list of paths separated by ";" from a minifilter.
config.txt file looks like:
\\Device\\HarddiskVolume2\\path1;\\Device\\HarddiskVolume2\\path2
Then, in instance_setup() method I do:
HANDLE fileHandle = NULL;
OBJECT_ATTRIBUTES objectAttributes;
PVOID result;
result = ExAllocatePool(NonPagedPool, 65536);
PFILE_OBJECT fileObject = NULL;
UNICODE_STRING myUnicodeStr;
RtlInitUnicodeString(&myUnicodeStr, config_file_path);
InitializeObjectAttributes(&objectAttributes,
&myUnicodeStr,
OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
NULL,
NULL);
IO_STATUS_BLOCK ioStatus;
FltCreateFile(flt_objects->Filter, flt_objects->Instance, &fileHandle, GENERIC_READ,
&objectAttributes, &ioStatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
FILE_OPEN, FILE_SEQUENTIAL_ONLY,
NULL, 0, 0);
ObReferenceObjectByHandle(fileHandle, GENERIC_READ, NULL, KernelMode,
&fileObject,
NULL);
ULONG bytes_read;
FltReadFile(flt_objects->Instance, fileObject, NULL, 65536, result,
FLTFL_IO_OPERATION_DO_NOT_UPDATE_BYTE_OFFSET,
&bytes_read, NULL, NULL);
FltClose(fileHandle);
//result should have the content of file
I am getting a blue screen when starting the minifilter. I´ve tested that the code works properly until FltCreateFile (I am able to get a proper fileHandler). I do not see what I am doing wrong after this.

poll the status of CTS line in windows

Hi stackoverflow users!
I need to poll the CTS line of my serial port in Windows environment,
I have opened successfully the COM port,
HANDLE hSerialIn;
const char* pcCommPort = TEXT("COM3");
hSerialIn = CreateFile(pcCommPort, GENERIC_READ | GENERIC_WRITE, \
0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Then I want to have something like this
DCB dcb = { 0 };
while (GetCommState(hSerialIn, &dcb)) {
if (dcb.fOutxCtsFlow)
;
else
;
}
The background of my interest in COM port is that there, I have a USB->UART convertor which in connected to the trigger output of the measuring device, this device triggers the output each second, and I want to have it in my program. When I connect to the COM port via Hercules(Terminal app) it works, I see that my CTS line is changing each second. So how to check the state of the CTS line?
Thanks in advance.
DWORD dwModemStatus;
BOOL fCTS = 0;
if (!SetCommMask(hSerialIn, EV_CTS))
{
DWORD err = GetLastError();
printf("\nHandle creation error code: %x\n", err);
}
DWORD dwCommEvent;
while(1)
{
if (!WaitCommEvent(hSerialIn, &dwCommEvent, NULL)) // An error occurred waiting for the event.
printf("");
else
{
if (!GetCommModemStatus(hSerialIn, &dwModemStatus)) // Error in GetCommModemStatus;
return;
fCTS = MS_CTS_ON & dwModemStatus;
if(fCTS)
printf("%x ", fCTS);
}
}

How to execute process memory?

I want to inject my code into some process and, I want to execute my injected code.
I can inject a code like this:
#include <Windows.h>
#define t_process_name example
BYTE code[7] = { 0x6A, 0x00 , 0, 0, 0, 0, 0};
/*
0:push 0
2:jmp 521595
*/
int main(){
HWND hWnd = NULL;
DWORD pid = NULL;
HANDLE hProcess = NULL;
BYTE* space = NULL;
BYTE* dst = 0x521595;
int i = 0;
hWnd = FindWindowA(NULL, t_process_name);
GetWindowThreadProcessId(hWnd, &pid);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, pid);
space = VirtualAllocEx(hProcess, NULL, sizeof(code), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
code[2] = 0xe9;
*(DWORD*)(code + 3) = (DWORD)(dst - (space+2)) - 5;
WriteProcessMemory(hProcess, space, code, sizeof(code), NULL);
// and...
}
But I don't know how to execute my injected code. (push 00 , jmp 521595 )
Are there any API or Way to do this?
If you know what you want to patch try diablo2oo2's Universal Patcher (windows only). It can create loader/patcher for your application. You're better off editing the source code directly.
edit: obviously this doesn't inject to other processes but if i may ask why would you do that?
If you know the exact assembly that needs to be executed you can do that with the asm
http://www.codeproject.com/Articles/15971/Using-Inline-Assembly-in-C-C

save file without save dialog

I have 3 global variables:
OPENFILENAME ofn;
TCHAR FileName[1024];
TCHAR Title[1024];
In WM_CREATE, I fill the information for ofn:
static TCHAR filter[] = TEXT("Bitmap Files (*.BMP)\0*.bmp\0") \
TEXT("All Files (*.*)\0*.*\0\0");
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = NULL;
ofn.lpstrFilter = filter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = NULL;
ofn.nMaxFile = 1024;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 1024;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = TEXT("bmp");
ofn.lCustData = 0;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
In WM_COMMAND with case ID_FILE_SAVE:, I have
case ID_FILE_SAVE:
if (!SaveAsBmpDialog(hwnd, FileName, Title))
return 0;
SaveAsBmp(FileName);
return 0;
where SaveAsBmpDialog is
static bool SaveAsBmpDialog(HWND hwnd, TCHAR *fileName, TCHAR *title)
{
ofn.hwndOwner = hwnd;
ofn.lpstrFile = fileName;
ofn.lpstrFileTitle = title;
ofn.Flags = NULL;
return GetSaveFileName(&ofn);
}
It works fine to save HDC to a Bitmap file (which can be open).
But in WM_CHAR with wParam == 'p', I have
SaveAsBmp(FileName);
return 0;
I use this function when SaveAsBmpDialog is called at once before, so FileName is a full-path already.
But I get a Bitmap file, which can not be open. (Even if I delete the file first, it creates the file, but still cannot be open). The file size is as before, so I think WriteFile in SaveAsBmp works.
I don't understand why it doesn't work without GetSaveFileName(&ofn) in WM_CHAR.
What I want is to save HDC to a exist file without save dialog.
ps: In SaveAsBmp, I have
HANDLE hFile = CreateFile(FileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwBytesWritten;
//SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
WriteFile(hFile, &fileHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, &info.bmiHeader, sizeof(BITMAPINFOHEADER) + info.bmiHeader.biClrUsed * sizeof(RGBQUAD), &dwBytesWritten, NULL);
WriteFile(hFile, pPixels, (int) info.bmiHeader.biSizeImage, &dwBytesWritten, NULL);
CloseHandle(hFile);

Resources