how to open partition with CreateFile API? - c++

I want to get handle from drive C:/.I can get handle from disk by:
HANDLE hd = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, NULL);
how to access partition of disk?
when use this code:
HANDLE hd = CreateFile("\\.\C", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, NULL);
and use GetLastError() error code is 123.
ERROR_INVALID_NAME
123 (0x7B)
The filename, directory name, or volume label syntax is incorrect.
from Microsoft doc:
image
but not work!
thanks in advance.

The name you should be using to open a volume is C: and make sure the back slashes are escaped appropriately.
HANDLE hd = CreateFile("\\\\.\\C:", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, NULL);
Note that you will probably get error 5 (Access denied), unless the exe is being run with administrator privileges.

Related

C++ create process command line executing powershell command in command line arguments

I want to create process with something like this:
LPTSTR cmdArgs = _T("C:\\Windows\\System32\\cmd.exe PowerShell -Command Add-AppxPackage -Path \"C:\\Users\\TEST2\\abc.appx\" ");
call the API as below:
CreateProcess(NULL, cmdArgs, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)
Here when I do so, application is goin into deadlock.

Printing on CTEXT widget in C++ MFC app

I'm working on pagination for my C++ app - I want to show currentPage / allPages on CTEXT and I'm having hard time making the printing functionality work.
This is how's widget configured in .rc file (I don't mind switching from CTEXT to any other
IDD_PIC_PLAYER_DIALOG DIALOG 0, 0, 400, 247
STYLE DS_SETFONT | DS_SETFOREGROUND | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_VISIBLE
EXSTYLE WS_EX_APPWINDOW | 0x80000000L
FONT 8, "MS Shell Dlg"
BEGIN
CTEXT "Static",IDC_PG,211,183,65,62
END
Widget has .bmp background I'm intending to be writing on.
IDB_PG BITMAP "res\\pg.bmp"
This is how my code looks like :
TCHAR szPageText[64] = TEXT("");
wsprintf(szPageText, TEXT("%d of %d"), iCurSelPage, m_iPageCount);
this->SetDlgItemText(IDC_PG, szPageText);
Code compiles, but does not doing what it's supposed to do - print those 2 integers.
If anybody knows what piece of puzzle am I missing here - I'd be grateful.
Thanks for your insight.

Can't grant others write access to my via open() created file

I am creating a file on linux using open()
mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int i = open(settingsPath.c_str(), O_CREAT | O_RDWR, perms);
As you can see I am specifiying Read/Write permissions for everyone.
But when I inspect the permissions in a terminal it says
-rw-rw-r-- 1 tstadler tstadler 0 Apr 17 10:54 settings.json
Why can't I give everyone write permissions?
Looks like write permissions to everyone are masked by current process' umask.
See man 2 umask

CreateNamedPipe causes ERROR_ACCESS_DENIED?

i need to create a named pipe for communication between client and server (in same host), here is the code:
WCHAR wszPipeName[MAX_FILE_LENGTH];
swprintf_s(wszPipeName, MAX_FILE_LENGTH, L"\\\\.\\pipe\\TEST%d", uniqueID);
pipe = CreateNamedPipe(
wszPipeName, // name of the pipe
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT,
1,
MAX_MSG_SIZE,
MAX_MSG_SIZE , //inbound buffer
MAX_READ_DATA_TIMEOUT,
NULL // use default security attributes
);
It the handler get back is always INVALID_HANDLE_VAULE, and the error is ERROR_ACCESS_DENIED.
Is there anything wrong here? It is running on Windows 7/8.
Thanks
This is Python code, but this sets up a security descriptor for the local user and denies remote:
dacl = ACL()
# Deny NT AUTHORITY\NETWORK SID
sid = CreateWellKnownSid(WinNetworkSid)
dacl.AddAccessDeniedAce(ACL_REVISION, GENERIC_ALL, sid)
# Allow current user SID
token = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY)
sid = GetTokenInformation(token, TokenUser)[0]
dacl.AddAccessAllowedAce(ACL_REVISION, GENERIC_READ | GENERIC_WRITE, sid)
security_descriptor = SECURITY_DESCRIPTOR()
security_descriptor.SetSecurityDescriptorDacl(True, dacl, False)
security_attributes = SECURITY_ATTRIBUTES()
security_attributes.SECURITY_DESCRIPTOR = security_descriptor
pipe = CreateNamedPipe(
<your other params here>
security_attributes
)
Found the reason, it is because the security limitation. After providing a suitable security descriptor, it just works!

How to hardcode a value in a texbox in c++

I am using the below code to popup the authentication dialog. I want to hard-code the password in my code and make it a read-only text box. What should I do?
IDD_LOGIN_AUTH_DIALOG DIALOG DISCARDABLE 0, 0, 148, 82
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Authentication"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "Host:",IDC_STATIC_HOST,7,8,36,10,SS_CENTERIMAGE |
NOT WS_GROUP
EDITTEXT IDC_VNCHOST,46,7,95,12,ES_AUTOHSCROLL | ES_READONLY |
NOT WS_TABSTOP
CONTROL 108,IDC_STATIC_LOGO,"Static",SS_BITMAP,7,23,32,30
RTEXT "User name:",IDC_STATIC_LOGIN,41,25,39,10,SS_CENTERIMAGE
EDITTEXT IDC_LOGIN_EDIT,84,24,57,12,ES_AUTOHSCROLL
RTEXT "Password:",IDC_STATIC_PASSWD,41,42,39,10,SS_CENTERIMAGE
EDITTEXT IDC_PASSWD_EDIT,84,41,57,12,ES_PASSWORD | ES_READONLY |
NOT WS_TABSTOP
DEFPUSHBUTTON "&OK",IDOK,20,61,50,14
PUSHBUTTON "&Cancel",IDCANCEL,77,61,50,14
END
You get full control over all fields if you use the CONTROL keyword rather than the EDITTEXT keyword, use
CONTROL "mypassword", IDC_PASSWD_EDIT, "Edit",
ES_LEFT | WS_BORDER | ES_PASSWORD | ES_READONLY,
84,41,57,12
instead of
EDITTEXT IDC_PASSWD_EDIT,84,41,57,12,ES_PASSWORD | ES_READONLY | NOT WS_TABSTOP
From memory:
HWND hwnd = ...//handle to the dialog
::SetDlgItemText(hwnd, IDC_STATIC_PASSWD, "YourText");
::EnableWindow(::GetDlgItem(hwnd, IDC_STATIC_PASSWD), FALSE);