Xcode sqlite3,use "insert" but fail and defined 1 - xcode

I wanted to add a line to my table 'Cloth',but failed.I then set breakpoint and found the defined is 1.
Here's the code.
char *addCloth = "INSERT INTO 'Cloth'(Ckind,Ccolor,Cseason,Ctexture,Cstyly) VALUES(?,?,?,?,?);";
sqlite3_stmt *stmt;
int success = sqlite3_prepare_v2(db, addCloth, -1, &stmt, nil);
if(success!= SQLITE_OK){
NSLog(#"Error: failed to insert:testTable");
sqlite3_close(db);
}
sqlite3_bind_text(stmt, 1, [strkind UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 2, [strcolor UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 3, [seas UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 4, [texture UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 5, [style UTF8String], -1, NULL);
success = sqlite3_step(stmt);
if(success != SQLITE_DONE)
{sqlite3_close(db);
NSLog(#"单品新增失败");
}
sqlite3_finalize(stmt);
}
and the run image:enter image description here
it seems my 'stmt' is nil,so I doubt whether if my 'strkind' and 'strcolor'is Chinese character caused it.
Or something else where made this error?
Thanks

Related

CreateNamedPipe failed with ERROR_ACCESS_DENIED for untrusted integrity process

I am implementing inter-process communication with named pipe, when it comes to untrusted integrity level processes(e.g. some child processes of chrome), CreateNamedPipe(https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea) fails with ERROR_ACCESS_DENIED. I wonder if there is anything wrong with security descriptor?
static VOID BuildDACL(PSECURITY_DESCRIPTOR pDescriptor)
{
PSID pSid;
EXPLICIT_ACCESS ea;
PACL pAcl;
SID_IDENTIFIER_AUTHORITY sia = SECURITY_WORLD_SID_AUTHORITY;
AllocateAndInitializeSid(&sia, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0,
&pSid);
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea.grfAccessPermissions = FILE_ALL_ACCESS;
ea.grfAccessMode = SET_ACCESS;
ea.grfInheritance = NO_INHERITANCE;
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea.Trustee.ptstrName = (LPTSTR)pSid;
if (SetEntriesInAcl(1, &ea, NULL, &pAcl) == ERROR_SUCCESS)
{
if (SetSecurityDescriptorDacl(pDescriptor, TRUE, pAcl, FALSE) == 0)
_tprintf(_T("[*] Failed to set DACL (%u)\n"), GetLastError());
}
else
_tprintf(_T("[*] Failed to add ACE in DACL (%u)\n"), GetLastError());
}
/* Create a SACL that will allow low integrity processes connect to our pipe. */
static VOID BuildSACL(PSECURITY_DESCRIPTOR pDescriptor)
{
PSID pSid;
PACL pAcl;
SID_IDENTIFIER_AUTHORITY sia = SECURITY_MANDATORY_LABEL_AUTHORITY;
DWORD dwACLSize = sizeof(ACL) + sizeof(SYSTEM_MANDATORY_LABEL_ACE) +
GetSidLengthRequired(1);
pAcl = (PACL)LocalAlloc(LPTR, dwACLSize);
InitializeAcl(pAcl, dwACLSize, ACL_REVISION);
AllocateAndInitializeSid(&sia, 1, SECURITY_MANDATORY_LOW_RID, 0, 0, 0, 0,
0, 0, 0, &pSid);
if (AddMandatoryAce(pAcl, ACL_REVISION, 0, SYSTEM_MANDATORY_LABEL_NO_WRITE_UP,
pSid) == TRUE)
{
if (SetSecurityDescriptorSacl(pDescriptor, TRUE, pAcl, FALSE) == 0)
_tprintf(_T("[*] Failed to set SACL (%u)\n"), GetLastError());
}
else
_tprintf(_T("[*] Failed to add ACE in SACL (%u)\n"), GetLastError());
}
static VOID InitSecurityAttributes(PSECURITY_ATTRIBUTES pAttributes)
{
PSECURITY_DESCRIPTOR pDescriptor;
pDescriptor = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR,
SECURITY_DESCRIPTOR_MIN_LENGTH);
InitializeSecurityDescriptor(pDescriptor, SECURITY_DESCRIPTOR_REVISION);
BuildDACL(pDescriptor);
BuildSACL(pDescriptor);
pAttributes->nLength = sizeof(SECURITY_ATTRIBUTES);
pAttributes->lpSecurityDescriptor = pDescriptor;
pAttributes->bInheritHandle = TRUE;
}
DWORD initIpc()
{
SECURITY_ATTRIBUTES sa;
InitSecurityAttributes(&sa);
HANDLE pipe = CreateNamedPipe(_MYIPC_NAME_, PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND, PIPE_WAIT, 1, sizeof(IPC_MESSAGE), sizeof(IPC_MESSAGE), 60 * 1000, &sa);
if (pipe == INVALID_HANDLE_VALUE)
{
// ERROR_ACCESS_DENIED
return 0;
}
...
}

ReportEvent returns error 'invalid parameter'

I wonder to set event into existed Event Log. These is code:
_ev_hndl = RegisterEventSource(NULL, L"Application");
WCHAR msg_[] = L"Hello";
if (!ReportEvent(_ev_hndl, ev_type, 1, 0, NULL, 1, 0, (LPCWSTR*)&msg_, NULL)) {
std::cout << "error: " << GetLastError() << std::endl;
}
But I get an error: 87 (invalid parameter)
The problem was close to msg parameter. Right code:
LPCTSTR strings[] = { TEXT("abcdefg") };
if (!ReportEvent(_ev_hndl, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, strings, NULL)) {...

INVALID_HANDLE_VALUE when using CreateFile(”\\.\C:“,...)

the code is here
HANDLE hDrive = CreateFile(_T("\\\\.\\D:"), GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,///*FILE_FLAG_WRITE_THROUGH |*/FILE_FLAG_NO_BUFFERING,
NULL);
assert(hDrive != INVALID_HANDLE_VALUE);
I also tried CreateFile(_T("\\\.\D:",...) and OPEN_EXISTING, but also return INVALID_HANDLE_VALUE.
I called GetLastError(), and the error code is 5, ERROR_ACCESS_DENIED.
thanks to #Barmak Shemirani and #Hans Passant,
I got a workable code
HANDLE hDrive = CreateFile(_T("\\\\.\\PhysicalDrive0"), GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY,///*FILE_FLAG_WRITE_THROUGH |*/FILE_FLAG_NO_BUFFERING,
NULL);
and
HANDLE hDrive = CreateFile(_T("\\\\.\\D:"), GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY,///*FILE_FLAG_WRITE_THROUGH |*/FILE_FLAG_NO_BUFFERING,
NULL);
DWORD dwError = GetLastError();
assert(hDrive != INVALID_HANDLE_VALUE);
cin.get();
need to run as administrator

Create Named Pipe C++ Windows

I am trying to create a simple comunication between 2 processes in C++ ( Windows ) like FIFO in linux.
This is my server:
int main()
{
HANDLE pipe = CreateFile(TEXT("\\\\.\\pipe\\Pipe"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
ConnectNamedPipe(pipe, NULL);
while(TRUE){
string data;
DWORD numRead =1 ;
ReadFile(pipe, &data, 1024, &numRead, NULL);
cout << data << endl;
}
CloseHandle(pipe);
return 0;
}
And this is my client:
int main()
{
HANDLE pipe = CreateFile(TEXT("\\\\.\\pipe\\Pipe"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
ConnectNamedPipe(pipe, NULL);
string message = "TEST";
DWORD numWritten;
WriteFile(pipe, message.c_str(), message.length(), &numWritten, NULL);
return 0;
}
The code does't work , how can i fixed it to like FIFO ?
You cannot create a named pipe by calling CreateFile(..).
Have a look at the pipe examples of the MSDN. Since these examples are quite complex I've quickly written a VERY simple named pipe server and client.
int main(void)
{
HANDLE hPipe;
char buffer[1024];
DWORD dwRead;
hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\Pipe"),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, // FILE_FLAG_FIRST_PIPE_INSTANCE is not needed but forces CreateNamedPipe(..) to fail if the pipe already exists...
1,
1024 * 16,
1024 * 16,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
while (hPipe != INVALID_HANDLE_VALUE)
{
if (ConnectNamedPipe(hPipe, NULL) != FALSE) // wait for someone to connect to the pipe
{
while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE)
{
/* add terminating zero */
buffer[dwRead] = '\0';
/* do something with data in buffer */
printf("%s", buffer);
}
}
DisconnectNamedPipe(hPipe);
}
return 0;
}
And here is the client code:
int main(void)
{
HANDLE hPipe;
DWORD dwWritten;
hPipe = CreateFile(TEXT("\\\\.\\pipe\\Pipe"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hPipe != INVALID_HANDLE_VALUE)
{
WriteFile(hPipe,
"Hello Pipe\n",
12, // = length of string + terminating '\0' !!!
&dwWritten,
NULL);
CloseHandle(hPipe);
}
return (0);
}
You should replace the name of the pipe TEXT("\\\\.\\pipe\\Pipe") by a #define which is located in a commonly used header file.

create toolbar with my bitmap images

this is an example code of msdn to create toolbar, but this example use the standard images of the system.
What do I need to change in this code to use my images from resource file, for example: IDB_COPY BITMAP "copy.bmp", and IDB_CUT BITMAP "cut.bmp", and IDB_PASTE BITMAP "paste.bmp".
HIMAGELIST g_hImageList = NULL;
HWND CreateSimpleToolbar(HWND hWndParent)
{
// Declare and initialize local constants.
const int ImageListID = 0;
const int numButtons = 3;
const int bitmapSize = 16;
const DWORD buttonStyles = BTNS_AUTOSIZE;
// Create the toolbar.
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0,
hWndParent, NULL, g_hInst, NULL);
if (hWndToolbar == NULL)
return NULL;
// Create the image list.
g_hImageList = ImageList_Create(bitmapSize, bitmapSize, // Dimensions of individual bitmaps.
ILC_COLOR16 | ILC_MASK, // Ensures transparent background.
numButtons, 0);
// Set the image list.
SendMessage(hWndToolbar, TB_SETIMAGELIST,
(WPARAM)ImageListID,
(LPARAM)g_hImageList);
// Load the button images.
SendMessage(hWndToolbar, TB_LOADIMAGES,
(WPARAM)IDB_STD_SMALL_COLOR,
(LPARAM)HINST_COMMCTRL);
// Initialize button info.
// IDM_NEW, IDM_OPEN, and IDM_SAVE are application-defined command constants.
TBBUTTON tbButtons[numButtons] =
{
{ MAKELONG(STD_FILENEW, ImageListID), IDM_NEW, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" },
{ MAKELONG(STD_FILEOPEN, ImageListID), IDM_OPEN, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"Open"},
{ MAKELONG(STD_FILESAVE, ImageListID), IDM_SAVE, 0, buttonStyles, {0}, 0, (INT_PTR)L"Save"}
};
// Add buttons.
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)numButtons, (LPARAM)&tbButtons);
// Resize the toolbar, and then show it.
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar, TRUE);
return hWndToolbar;
}
I found this solution:
const int ID_TB_STANDARD = 0;
const int ID_IL_STANDARD = 0;
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | TBSTYLE_TOOLTIPS, 0, 0, 0, 0, hWnd, (HMENU)ID_TB_STANDARD, hInstance, NULL);
HIMAGELIST hImageList = ImageList_LoadBitmap(hInstance, MAKEINTRESOURCEW(IDB_CUT), 16, 0, RGB(255, 0, 255));
ImageList_Add(hImageList, LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_COPY)), NULL);
ImageList_Add(hImageList, LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_PASTE)), NULL);
SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)ID_IL_STANDARD, (LPARAM)hImageList);
SendMessage(hWndToolbar, (UINT) TB_SETHOTIMAGELIST, 0, (LPARAM)hHotImageList);
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
TBBUTTON tbb[3] =
{
{0,ID_CUT,TBSTATE_ENABLED,TBSTYLE_BUTTON},
{1,ID_COPY,TBSTATE_ENABLED,TBSTYLE_BUTTON},
{2,ID_PASTE,TBSTATE_ENABLED,TBSTYLE_BUTTON},
};
SendMessage(hWndToolbar, (UINT) TB_ADDBUTTONS, 3, (LPARAM)&tbb);
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar , SW_SHOW);

Resources