Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is my current MD5 function. When used on windows 8.1 it always returns a value, but when run on windows 7 it only returns a value about 50% of the time. That is pretty peculiar to me. Any ideas?
It turns out that it is dodgy on both win7 and win8. Apparently the call to CryptGetHashParam sometimes fails with ERROR_MORE_DATA.
std::string MD5(std::string input)
{
HCRYPTPROV CryptProv;
HCRYPTHASH CryptHash;
BYTE BytesHash[33];//!
DWORD dwHashLen;
std::string final;
if (CryptAcquireContext(&CryptProv,NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
if (CryptCreateHash(CryptProv, CALG_MD5, 0, 0, &CryptHash))
{
if (CryptHashData(CryptHash, (BYTE*)input.c_str(), input.length(), 0))
{
if (CryptGetHashParam(CryptHash, HP_HASHVAL, BytesHash, &dwHashLen, 0))
{
final.clear();
std::string hexcharset = "0123456789ABCDEF";
for (int j = 0; j < 16; j++)
{
final += hexcharset.substr(((BytesHash[j] >> 4) & 0xF), 1);
final += hexcharset.substr(((BytesHash[j]) & 0x0F), 1);
}
}
}
}
}
CryptDestroyHash(CryptHash);
CryptReleaseContext(CryptProv, 0);
return final;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *pp = "derp";
std::string derp = MD5(std::string(pp));
printf("%s\n", derp.c_str());
system("pause");
return 0;
}
I have a fantastic idea: add else clauses to those conditions so that you know which one is failing. Put output in those clauses so that you can see detail on the error conditions. Then you will know what is wrong with your function.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
There are 2 message queues and 2 queue ids. When I call msgrcv, the queue id that is not associated with this msgrcv call gets altered. Its declared globally. When I make it static global the value does not change. But I need to know the root cause of this issue. The code snippet is below
int xmlrpcqid;
rtspbuf rpcbuf;
int statusqid;
msqbuf sbuf;
static void *getXmlResponse(void *arg);
printf ("*******msg qid position 1 = %d\n", statusqid);
while(!eventLoopWatchVariable)
{
printf ("*******msg qid position 2 = %d \n", statusqid);
if(msgrcv(xmlrpcqid, &rpcbuf, sizeof(rtspbuf), 0, MSG_NOERROR) < 0)
{
printf("msgrcv Fail \n");
}
else {
printf ("*******msg qid 3 = %d \n", statusqid);
}
}
// not adding remaining code
}
int main {
if ((xmlrpcqid = msgget (1256, IPC_CREAT | 0666 )) < 0)
printf ("login msgget failed\n");
if ((statusqid = msgget (1234, IPC_CREAT | 0666 )) < 0)
printf ("status msgget failed\n");
pthread_t xmlResponse_thr = -1;
if(pthread_create(&xmlResponse_thr, NULL, getXmlResponse, NULL))
{
printf("pthread_create failed for getXmlResponse\n");
return -1;
}
}
In Linux, we can use the function stat() to get a file info, and use the type st.mode to judge the rights of the file before we can do some operation on it. In windows, I make a lot of attempts, but little help.
At first, I want to use the function GetSecurityInfo, but I can't get the handle argument. I did find some solutions, but they all need use fopen function which is exactly what I want to avoid. Becasue I want to not do anything substantial with the file until I can determine what permissions it has.
Then I try the GetFileSecurityA function, but useless. The following is my code, and I get an error code 998 from getlasterror
void GetFilesInfo(std::string& path)
{
char *path1 = new char[1024];
strcpy(path1, path.c_str());
SECURITY_INFORMATION FLAGS = ATTRIBUTE_SECURITY_INFORMATION;
PSECURITY_DESCRIPTOR file_security_descriptor = new char[1024];
LPDWORD minNeedWords = 0;
if(GetFileSecurityA(path1, FLAGS, file_security_descriptor, 1024, minNeedWords) == 0)
error_die("GetFileInfo");
else
std::cout << file_security_descriptor << std::endl;
}
The answer is as previous comments said. I answered the question for completion.
#include <Windows.h>
void main()
{
TCHAR FileName[] = {L"C:\\Users\\Path\\To\\FileName.extension" };
DWORD LengthNeeded = 0;
SECURITY_DESCRIPTOR* sp = (SECURITY_DESCRIPTOR*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,100);
BOOL rs = GetFileSecurity(FileName, ATTRIBUTE_SECURITY_INFORMATION, sp,100,&LengthNeeded);
if (!rs)
{
DWORD e = GetLastError();
//return;
}
HeapFree(GetProcessHeap(), 0,sp);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i don't know why parameters passed to pthread changes automatically.
i'll show you my code
the debug result is this :
begin of while while() =begin= 0 ===
params: a84a5310
->socket: a84a4e70
->connection: a84a52d0(4)
->webserver: a84a4ea0(0)
===== begin of while while() =end====
===== end of while() =begin= 1709692672 ===
params: a84a5310
->socket: a84a4e70
->connection: a84a52d0(4)
->webserver: a84a4ea0(0)
===== end of while() =end====
Accepting...
===== in thread() =begin= 1709692672 ===
params: aacf4240//wrong expected a84a5310
->socket: a84a5310 //wrong expected a84a4e70
->connection: a84a4ea0(0)//wrong expected a84a52d0(4)
->webserver: 65e7d700(0) //wrong expected a84a4ea0(0)
===== in thread() =end====
this is struct that i pass to the thread :
typedef struct InfoServer{
ServerTCP* server;
WebServer* webserver;
ConnServer* serverConn;
}ServerInfo;
this is debug function :
void* show_params(ServerInfo* params, char* where, int n) {
printf("===== %s =begin= %d ===\n",where, n);
printf("params: %x\n",
params);
printf("->socket: %x\n",
params->server);
printf("->connection: %x(%d)\n",
params->serverConn, params->serverConn->getConn());
printf("->webserver: %x(%d)\n",
params->webserver);
printf("===== %s =end====\n",where);
fflush(stdout);
}
this is main function :
void *manageConnection(void *serverConn);
void fineRichiesta(ServerInfo * serverinformation);
int main (int argc , char* argv[]){
if(argc!=2){
printf("USAGE: %S , PORT \n",argv[0]);
fflush(stdout);
exit(1);
}
int port = atoi(argv[1]);
ServerTCP* tserver= new ServerTCP(port);
WebServer* webserv= new WebServer();
bool exit = false;
while( true ){
pthread_t manageConnection_thread;
ConnServer* serverC= tserver->acceptConn();
ServerInfo* serverinfo = (ServerInfo*) malloc(sizeof(ServerInfo));
serverinfo->server = tserver;
serverinfo->webserver = webserv;
serverinfo->serverConn =serverC;
show_params(serverinfo,"starting in while()",0);
if(pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) &serverinfo)) {
error("Error Creating server");
}
show_params(serverinfo,"end of while()", manageConnection_thread);
}
delete( tserver);
delete( webserv);
return 0;
}
here is thread code :
void *manageConnection(void * serverinformation){
ServerInfo *serverinfo = (ServerInfo*)serverinformation;
show_params(serverinfo,"in thread()", pthread_self());
ConnServer * srvConn = serverinfo->serverConn;
char* answ;
if(srvConn){
answ = (char*)srvConn->riceviServer();
}
else {
printf("im in else\n");
fflush(stdout);
}
if(!answ){
printf("finerichiesta\n");
fflush(stdout);
fineRichiesta(serverinfo);
}
printf("answ: %s\n", answ);
fflush(stdout);
char* file = serverinfo->webserver->getFile(answ);
srvConn->inviaServer(file);
fineRichiesta( serverinfo);
free(answ);
return NULL;
}
function :
void fineRichiesta(ServerInfo * serverinformation){
printf("im in fine richiesta\n");
fflush(stdout);
serverinformation->server->disconnect(serverinformation->serverConn);
free(serverinformation);
pthread_exit(NULL);
}
Any help would be amazing , im getting crazy -
i dont know if my question is best formulated -
thanks in advance ...
Your pthread_create call is passing the wrong void * value to the thread start routine manageConnection.
It is passing the address of the serverInfo variable:
pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) &serverinfo)
It should be passing value of the serverInfo variable, which points to a filled in ServerInfo:
pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) serverinfo) // <-- note: ampersand removed here
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to figure out how to adapt this sample in order to redirect the output of a console window to a text box inside another process.
Unfortunately, it seems that the reader will never receive any input.
Further debugging shows that the call to SetHandleInformation always aborts with Error 6: Invalid Handle. The value of hPipeOutRd does not look bad, it is something like 0x00000244.
This reproduces the matter:
int main(int argc, char *argv[])
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
int result = 0;
HANDLE hPipeOutRd = INVALID_HANDLE_VALUE; // This end is passed to the pipe reader
HANDLE hPipeOutWr = INVALID_HANDLE_VALUE; // This end is passed to the child process
if ( result == 0 && !::CreatePipe( &hPipeOutRd, &hPipeOutWr, &sa, 4096 ) )
{
result = -1;
printf("Error: %u\r\n", GetLastError() );
}
if ( result == 0 && !::SetHandleInformation( &hPipeOutRd, HANDLE_FLAG_INHERIT, 0 ) ) // This fails with invalid handle
{
result = -1;
printf("Error: %u\r\n", GetLastError() );
}
return result;
}
Any ideas why?
A HANDLE is already a pointer. You don't take its address unless it is an out parameter.
Just take the & out of your SetHandleInformation call.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new in linux device driver. I wan to write a C/C++ code to perform file transfer from raspberry pi to usb flash drive. I having difficult for the starting point, so i try on libusb for HID device sample code from signal11 and the code works fine for detecting my optical mouse with its device ID. Then i try to obtain usb flash drive vendor id somehow it give me very wired number. Finally i come out with a very silly try out by writing a bash script for cp a file to usb flash drive and activate the script in C++ and it works but i feel it is not a proper way to do it. Then i start with SCSI protocol and i very hard to understand how it works.Any guideline is appreciated.
int scsi_get_serial(int fd, void *buf, size_t buf_len) {
// we shall retrieve page 0x80 as per http://en.wikipedia.org/wiki/SCSI_Inquiry_Command
unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, buf_len, 0};
unsigned char sense[32];
struct sg_io_hdr io_hdr;
int result;
memset(&io_hdr, 0, sizeof (io_hdr));
io_hdr.interface_id = 'S';
io_hdr.cmdp = inq_cmd;
io_hdr.cmd_len = sizeof (inq_cmd);
io_hdr.dxferp = buf;
io_hdr.dxfer_len = buf_len;
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
io_hdr.sbp = sense;
io_hdr.mx_sb_len = sizeof (sense);
io_hdr.timeout = 5000;
result = ioctl(fd, SG_IO, &io_hdr);
if (result < 0)
return result;
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
return 1;
return 0;
}
int main(int argc, char** argv) {
//char *dev = "/dev/sda";
char *dev = "/dev/sg2";
char scsi_serial[255];
int rc;
int fd;
fd = open(dev, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
perror(dev);
}
memset(scsi_serial, 0, sizeof (scsi_serial));
rc = scsi_get_serial(fd, scsi_serial, 255);
// scsi_serial[3] is the length of the serial number
// scsi_serial[4] is serial number (raw, NOT null terminated)
if (rc < 0) {
printf("FAIL, rc=%d, errno=%d\n", rc, errno);
} else
if (rc == 1) {
printf("FAIL, rc=%d, drive doesn't report serial number\n", rc);
} else {
if (!scsi_serial[3]) {
printf("Failed to retrieve serial for %s\n", dev);
return -1;
}
printf("Serial Number: %.*s\n", (size_t) scsi_serial[3], (char *) & scsi_serial[4]);
}
close(fd);
return (EXIT_SUCCESS);
}
I get this serial number: 00/1F
Then i try write this in test.sh
cp /home/Desktop/stl4.pdf /media/mini_flash
and run system("./test.sh") in C++
The question seems contradictory, at first you say you want to copy a file using a kernel driver, which seems strange to say the least. Then you say you use libusb, which is an userspace library. Then you say that you try to execute a shell script with cp.
Maybe what you want is simply a code snippet that copies a file form an userspace C/C++ program? Try one of these snippets.
In detail, if all you want to do is a C++ equivalent of cp /home/Desktop/stl4.pdf /media/mini_flash, then this is enough:
ifstream in("/home/Desktop/stl4.pdf",ios::binary);
ofstream out("/media/mini_flash/stl4.pdf",ios::binary);
out<<in.rdbuf();
in.close();
out.close();