Posted in Uncategorized, Service, Windows

Windows 10 | move workspace shortcut

So cool function is Windows 10. It is so light performance in Windows 10.
Move workspace quickly and enhance the performance.
When using Cygwin, ‘Ctrl + tab’ can move Cygwin windows. It also so nice to work.

Windows Key + Ctrl + D : Add a new workspace
Windows Key + Ctrl + ← / → / ↑ / ↓ : Move(Navigate) worksapce

Advertisements
Posted in Service, Windows

C# | Access Violation When Using Shared Memory From C/C++

When using dll made in C/C++ with the API below, a program in C# occurs ‘Access Violation’.

CreateFileMapping
OpenFileMapping
MapViewOfFile

 
The below doesn’t work. So message passing with named pipe is in the air instead of shared memory.

 
 
———————————————————————-
 
It can be resolved by using another API in C#.

 

[Resolution]
Apply this

                for (int i = 0; i < size; i++)
                    rawDataArray[i] = Marshal.ReadByte(rawData + i);

Instead of the below.

                //Marshal.Copy(rawData, rawDataArray, 0, size);

 

[Shared Memory Example]

	SECURITY_ATTRIBUTES SecAttr, *pSec = 0;
	SECURITY_DESCRIPTOR SecDesc;

	if (InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION) &&
		SetSecurityDescriptorDacl(&SecDesc, TRUE, (PACL)0, FALSE))
	{
		SecAttr.nLength = sizeof(SecAttr);
		SecAttr.lpSecurityDescriptor = &SecDesc;
		SecAttr.bInheritHandle = TRUE;
		pSec = &SecAttr;
	}

	live_h_map_file = CreateFileMapping(
		INVALID_HANDLE_VALUE,    // use paging file
		pSec,                    // default security
		PAGE_READWRITE,          // read/write access
		0,                       // maximum object size (high-order DWORD)
		sizeof(LIVE_DATA),                // maximum object size (low-order DWORD)
		LIVE_SHARED_MAP_OBJ_NAME);                 // name of mapping object

	if(!live_h_map_file)
	{
		cs_log("Could not set shared memory for live capture variable.\n");
		return -1;
	}

*h_map_file = OpenFileMapping(
		FILE_MAP_WRITE, //FILE_MAP_ALL_ACCESS,   // read/write access
		FALSE,                 // do not inherit the name
		LIVE_SHARED_MAP_OBJ_NAME);                 // name of mapping object

	if(!(*h_map_file)) {
		cs_log("Could not get shared memory for live catpure.\n");
		return -1;
	}
	*live_data = (LIVE_DATA *) MapViewOfFile(*h_map_file,   // handle to map object
		FILE_MAP_WRITE, // FILE_MAP_ALL_ACCESS, // read&write permission
		0,
		0,
		sizeof(LIVE_DATA));

	if(!(*live_data)) {
		cs_log("Could not set shared live catpture.\n");
		CloseHandle(*h_map_file);
		return -2;
	}
Posted in Service, Windows

Reinstall A Program Forcely

If uninstallation by program features in control panel does not work, it is helpful to remove it from register and reinstall the program to uninstall again properly by a well-working reinstalled program.
Remove a program registry file from the below path.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
HKEY_CLASSES_ROOT\Installer\Products
Posted in Service, Windows

Windows admin in C : How to know whether current process has administrator and execute the process a administrator privilege in C?

Windows. How to know(check) the administrator privilege(right, permission) of the program in C code(source) ?

The example source is as the below. It checks the administrator privilege of the program and if it doesn’t have, it restart the program as the administrator permission.

 

 


#include <windows.h>

int init_admin_run(void)
{
    char path[MAX_PATH] = { 0 };

    if(is_admin_run())
        return 1;

    GetModuleFileName(NULL, path, MAX_PATH);
    ShellExecute(NULL, "runas", path, NULL, NULL, SW_SHOWNORMAL);
    exit(0);

    return 0;
}

int is_admin_run(void)
{

    BOOL   fReturn         = FALSE;
    DWORD  dwStatus;
    DWORD  dwAccessMask;
    DWORD  dwAccessDesired;
    DWORD  dwACLSize;
    DWORD  dwStructureSize = sizeof(PRIVILEGE_SET);
    PACL   pACL            = NULL;
    PSID   psidAdmin       = NULL;

    HANDLE hToken              = NULL;
    HANDLE hImpersonationToken = NULL;

    PRIVILEGE_SET   ps;
    GENERIC_MAPPING GenericMapping;

    PSECURITY_DESCRIPTOR     psdAdmin           = NULL;
    SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;


    /*
       Determine if the current thread is running as a user that is a member 

       of
       the local admins group.  To do this, create a security descriptor 

       that
       has a DACL which has an ACE that allows only local aministrators 

       access.
       Then, call AccessCheck with the current thread's token and the 

       security
       descriptor.  It will say whether the user could access an object if 

       it
       had that security descriptor.  Note: you do not need to actually 

       create
       the object.  Just checking access against the security descriptor 

       alone
       will be sufficient.
     */
    const DWORD ACCESS_READ  = 1;
    const DWORD ACCESS_WRITE = 2;



    /*
       AccessCheck() requires an impersonation token.  We first get a 

       primary
       token and then create a duplicate impersonation token.  The
       impersonation token is not actually assigned to the thread, but is
       used in the call to AccessCheck.  Thus, this function itself never
       impersonates, but does use the identity of the thread.  If the 

       thread
       was impersonating already, this function uses that impersonation 

       context.
     */
    if (!OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, 

                TRUE, &hToken))
    {
        if (GetLastError() != ERROR_NO_TOKEN)
            return 0;;

        if (!OpenProcessToken(GetCurrentProcess(), 

                    TOKEN_DUPLICATE|TOKEN_QUERY, &hToken))
            return 0;;
    }

    if (!DuplicateToken (hToken, SecurityImpersonation, 

                &hImpersonationToken))
        return 0;;


    /*
       Create the binary representation of the well-known SID that
       represents the local administrators group.  Then create the 

       security
       descriptor and DACL with an ACE that allows only local admins 

       access.
       After that, perform the access check.  This will determine whether
       the current user is a local admin.
     */
    if (!AllocateAndInitializeSid(&SystemSidAuthority, 2,
                SECURITY_BUILTIN_DOMAIN_RID,
                DOMAIN_ALIAS_RID_ADMINS,
                0, 0, 0, 0, 0, 0, &psidAdmin))
        return 0;;

    psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
    if (psdAdmin == NULL)
        return 0;;

    if (!InitializeSecurityDescriptor(psdAdmin, 

                SECURITY_DESCRIPTOR_REVISION))
        return 0;;

    // Compute size needed for the ACL.
    dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) +
        GetLengthSid(psidAdmin) - sizeof(DWORD);

    pACL = (PACL)LocalAlloc(LPTR, dwACLSize);
    if (pACL == NULL)
        return 0;;

    if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2))
        return 0;;

    dwAccessMask= ACCESS_READ | ACCESS_WRITE;

    if (!AddAccessAllowedAce(pACL, ACL_REVISION2, dwAccessMask, 

                psidAdmin))
        return 0;;

    if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE))
        return 0;;

    /*
       AccessCheck validates a security descriptor somewhat; set the 

       group
       and owner so that enough of the security descriptor is filled out 

       to
       make AccessCheck happy.
     */
    SetSecurityDescriptorGroup(psdAdmin, psidAdmin, FALSE);
    SetSecurityDescriptorOwner(psdAdmin, psidAdmin, FALSE);

    if (!IsValidSecurityDescriptor(psdAdmin))
        return 0;;

    dwAccessDesired = ACCESS_READ;

    /*
       Initialize GenericMapping structure even though you
       do not use generic rights.
     */
    GenericMapping.GenericRead    = ACCESS_READ;
    GenericMapping.GenericWrite   = ACCESS_WRITE;
    GenericMapping.GenericExecute = 0;
    GenericMapping.GenericAll     = ACCESS_READ | ACCESS_WRITE;

    if (!AccessCheck(psdAdmin, hImpersonationToken, dwAccessDesired,
                &GenericMapping, &ps, &dwStructureSize, &dwStatus,
                &fReturn))
    {
        fReturn = FALSE;
        return 0;;
    }
    // Clean up.
    if (pACL) LocalFree(pACL);
    if (psdAdmin) LocalFree(psdAdmin);
    if (psidAdmin) FreeSid(psidAdmin);
    if (hImpersonationToken) CloseHandle (hImpersonationToken);
    if (hToken) CloseHandle (hToken);

    return fReturn;
}