c - Windows API MoveFile() not working for running exe

Here is a simple C program for illustration:#include <windows.h>#include <stdio.h>int main(int argc, char *argv[]){ //MoveFile(argv[0], "dst.exe"); getchar(); return 0;}make an test.exe from code above. Now execute test.exe, the test.exe hangs there due to getchar(), then I can cut and paste this exe freely.But when I uncomment that MoveFile(argv[0], "dst.exe");, I was hoping it could move itself to dst.exe, it turns out to have a dst.exe, while program.exe is still there, just like CopyFile() does.From what I know, in Win...Read more

c - Run a portable executable in memory - WinApi

Here is the code I currently have:#include <Windows.h>DWORD run_portable_executable(unsigned char* binary){ BOOL success; const DWORD binary_address = (DWORD)binary; IMAGE_DOS_HEADER* const dos_header = (LPVOID)binary; IMAGE_NT_HEADERS* const nt_header = (LPVOID)(binary_address + dos_header->e_lfanew); STARTUPINFOW startup_info; PROCESS_INFORMATION process_info; // Zero the structs to ensure valid values. SecureZeroMemory(&startup_info, sizeof(startup_info)); SecureZeroMemory(&process_info, sizeof(pro...Read more

c - Which .lib has IID___x_ABI_CWindows_CSystem_CProfile_CSystemManufacturers_CISmbiosInformationStatics?

The windows headers for winrt, specificallywinrt\windows.system.profile.systemmanufacturers.h contain among other things, a reference to:EXTERN_C const IID IID___x_ABI_CWindows_CSystem_CProfile_CSystemManufacturers_CISmbiosInformationStatics;However as far as I can find, that IID isn't defined in any known linker library. Does anyone happen to know where it is defined?I'm trying to use this from C to call the winrt apis and getting an unresolved symbol....Read more

C split pointer array and save in new var

I'm very busy with splitting and saving the result for a 2d pointer array in another smaller 2d pointer array. However, I cannot get this working; only the first line matches. Can someone help me or tell me what I am doing wrong? Code:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdbool.h>#include <time.h>#include <sys/time.h>void print_grid(bool *grid, int n, int m){ int i, j; for (i = 0; i < n; i++){ for (j = 0; j < m; j++){ printf("%d ", (bool)*((grid+i*n)...Read more

c - How to use divide and conquer and the fact that if one subarray has a majority, the combined array has a majority to find majority element?

In the question we were told that the crux of the algorithm is the fact that"When we get down to single elements, that singleelement is returned as the majority of its (1-element) array. At every other level, it will get return values from itstwo recursive calls. The key to this algorithm is the fact that if there is a majority element in the combined array,then that element must be the majority element in either the left half of the array, or in the right half of the array."My implementation was this, probably very buggy but the general idea w...Read more

Parsing nmea csv in c++/c for microchip

How can I parse a comma separated char string? I've tried using strtok but I can't get it working. char str2[] = "$GNRMC,011802.00,A,4104.22420,N,08131.66173,W,0.021,,280218,,,D*78\n"; char *p; p = strtok(str2, ","); char *input[8]; int i = 0;for( i=0;i<8;i++) { input[i] = p; p = strtok(NULL, ","); }Ideally I'd like to be able to set a variable to the string. Such asif (i == 0){string type = $GNRMC;}if (i == 1){float thisnum = 011802.00}etc.This is being written for a pic so I can't use vectors....Read more

c - Checking if a string exists using a substring (using linked lists)

I need to make a function that receives a substring as an argument and checks if any of the strings in a linked list contain that substring. The function creates a new linked list with all of the strings that contain the substring given.The function looks like this: lista *lista_pesquisa_substring(lista *lst, char *substring){ l_elemento *aux, *curr; lista *lis2; int i = 0, tamanho; lis2 = lista_nova(); if(lst == NULL || substring == NULL){ return NULL; } for (aux = lst->inicio; aux != NULL; aux = aux->p...Read more

c - Improve INSERT-per-second performance of SQLite?

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second!Background: We are using SQLite as part of a desktop application. We have large amounts of configuration data stored in XML files that are parsed and loaded into an SQLite database for further processing when the application is initialized. SQLite is ideal for this situation because it's fast, it requires no specialized configuration, and the database is stored on disk as a single file.Rationale: Initiall...Read more

c - Drag-and-drop from 32 to 64-bit

I am writing a C program that accepts drag-and-drop of files.When it is compiled in 32-bit, it works in any case. But when it compiled in 64-bit, it works only for files dragged from a 64-bit application:32-bit -> 32-bit : success64-bit -> 64-bit : success64-bit -> 32-bit : success32-bit -> 64-bit : failI still get the WM_DROPFILES message, but DragQueryFile returns nothing (the number of files is 0).This seems to be an issue for a lot of applications but I would like to know if there is a workaround about that.Edit:If I drag-and-drop a file fr...Read more

c - Why does this function always crash when free memory?

int gb2Utf8(const char* source, int sourceLen, void *target, int targetLen){ int result = 0; int bufLen = strlen(source) * 2; wchar_t *buffer = (wchar_t *)malloc(bufLen); if (!buffer) { result = 1; goto RETURN; } //GB18030 code page: 54936 int m2wResult = MultiByteToWideChar(54936, MB_ERR_INVALID_CHARS, source, -1, buffer, bufLen); if (!m2wResult) { result = 2; goto RETURN; } int w2mResult = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, buffer, -1, (char *)target, targetLen, N...Read more

c - How to make an OpenGL rendering context with transparent background?

Rendering contexts usually have a solid color on the background (black or whatever, see the image below):I'm wondering if it's possible to setup a window, with no decorations AND with the transparent background, while allowing me to render OpenGL stuff on it.This would give the illusion that the triangle is floating on the screen. The transparent background should allow you to see the desktop or other applications that might be behind it.Could you please exemplify with source code?Platform: Windows (win32 only)...Read more

c - What is __stdcall?

I'm learning about Win32 programming, and the WinMain prototype looks like:int WINAPI WinMain ( HINSTANCE instance, HINSTANCE prev_instance, PSTR cmd_line, int cmd_show )I was confused as to what this WINAPI identifier was for and found:#define WINAPI __stdcallWhat does this do? I'm confused by this having something at all after a return type. What is __stdcall for? What does it mean when there is something between the return type and function name?...Read more

c - SHCreateItemFromParsingName return FILE_NOT_FOUND when filename specified

I try get IShellItem for a file to copy it with IFileOperation COM interface from system directory to another directory. I must use exactly IFileOperation COM interface for this purpose.When I specify full filename - return value from SHCreateItemFromParsingName() was ERROR_FILE_NOT_FOUND, but file present in the directory. When I delete filename from path below and use only folder path - all seems good, return value is S_OK.//...CoInitialize(NULL);//...WCHAR szSourceDll[MAX_PATH * 2];wcscpy_s(szSourceDll, MAX_PATH, L"C:\\Windows\\System32\\sys...Read more

c - How to display a pop-up message box from a driver (kernel mode)?

I'm writing a driver which needs to immediately pop up a dialog to notify the user of an event.(Kind of similar to NTFS's "Corrupt file" notification, except that this is not a filesystem-related driver.)I know ExRaiseHardError and IoRaiseInformationalHardError should be able to do the trick, but they don't seem to work -- they return "successfully" without actually doing anything.How do I go about doing this (without creating a user-mode program)?A user-mode version of the code (which works correctly) is below.In the kernel-mode version, I cal...Read more

c - How to delete a file from kernel-mode?

I have a minifilter (kernel-mode). I want to delete a file with specific path (\Device\HarddiskVolume1\file.txt or C:\file.txt) from kernel-modeIs there any way to do that?UPDATE: 20150130I try to use ZwDeleteFile routine as Harry Johnston said. These are my codes:RtlInitUnicodeString(&gRedirectFullFilePath, "\\Device\\HarddiskVolume1\\test.txt"); // This file existedInitializeObjectAttributes(&ObjectAttribute, &gRedirectFullFilePath, OBJ_CASE_INSENSITIVE, NULL, NULL); status = ZwDeleteFile(&ObjectAttribute);But it crash my syst...Read more