CFileDialog constructor can't spot TRUE and fopen(str,"r") error - fopen

I call the CFileDialog constructor in the following method:
CFileDialog FileDlg(TRUE, ".txt", NULL, 0, strFilter);
according to the parameters in the CFileDialog constructor, the first parameter should be BOOL, but my compiler thinks it's int instead of BOOL. Can you tell me why?
Besides, in the code, when I use fopen(str,"r"), the error is no conversion function from CSring to const char*. Appreciate your discussion.
The code is:
void OnFileOpen()
{
CClientDC dc(this);
CString str;
CRect rc;
FILE *ifp;
char strFilter[] = { "TXT Files (*.txt)|*.txt|All Files(*.*)|*.*||" };
CFileDialog FileDlg(TRUE, ".txt", NULL, 0, strFilter);
if (FileDlg.DoModal() == IDOK)
{
str = FileDlg.GetFileName();
ifp = fopen(str,"r");
dc.TextOutW(350, 50, "File Opened: " + str);
for (int i = 1; i < n; i++)
{
fscanf(ifp, "%d %d", &pt[i].x, &pt[i].y);
rc = CRect(pt[i].x - 30, pt[i].y - 30, pt[i].x + 30, pt[i].y + 30);
dc.Ellipse(rc);
rc = CRect(pt[i].x - 1, pt[i].y - 1, pt[i].x + 1, pt[i].y + 1);
dc.Rectangle(rc);
}
fclose(ifp);
}
}

Related

cannot convert 'const wchar_t*' to 'LPCSTR' {aka 'const char*'}

Please help me with this code, I don't know how to change it so I can fix it and run it
std::wstring stemp = std::wstring(filename.begin(), filename.end());
hBitmapG = (HBITMAP)LoadImage(GetModuleHandle(NULL), stemp.c_str(), IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE);
if (!hBitmapG) {
DWORD err = GetLastError();
std::wstring errorMsg = L"Failed to LoadImage - '" + stemp + L"', error code (" + std::to_wstring((long long)err) + L")";
MessageBox(NULL, errorMsg.c_str(), L"WinCanvas::DrawBitmap()", MB_OK);
PostMessage(hWnd, WM_DESTROY, NULL, NULL); // Post a message to destroy (shutdown) the program
return FALSE;
}
You can use MessageBoxW function to use wide characters in MessageBox.

Protractor need to break from For LOOP when if is executed

Can anyone Please Help me in this in the below code, my requirement is once If is true, I should come out from For loop
===================================================
for (i = 0; i < 50; i++) {
element(By.xpath("//*[text() = '" + InvoiceREFnumberCompany + "']")).isPresent().then(function(result) {
if (result )
{
element(By.xpath("//*[text() = '" + InvoiceREFnumberCompany + "']")).click();
break;
}
else
{
obj1.ClickOnRefreshButton.click();
}
});
}
Output
Failures:
NPOInvoice-Fire -- NPO Fire Collabrate with PTC user
Message:
[31m Failed: Illegal break statement[0m
Stack:
SyntaxError: Illegal break statement

ICMP packet obsolete or malformed

I'm trying to use RAW sockets to create a ping program. The program I wrote however works only for localhost(127.0.0.1) and does not work for other IP addresses. While analyzing the generated packet in Wireshark, I get a message saying "Unknown ICMP (obsolete or malformed?)". It also tells me that the ICMP checksum is incorrect. I'm posting my code here
char source[20], destination[20];
struct sockaddr_in src, dst;
int addrlen= sizeof(src), recvSize, packetSize;
char *packet;
char *buffer;
struct iphdr* ip;
struct iphdr* ip_reply;
struct icmphdr* icmp;
unsigned short csum(unsigned short *, int);
char* getip();
int main(int argc, char* argv[]){
int pingSocket, optval;
struct protoent *protocol;
if(*(argv + 1) && (!(*(argv + 2)))){
//only one argument provided,assume it is the destination server
strncpy(destination, *(argv + 1), 15);
strncpy(source, getip(), 15);
}
inet_pton(AF_INET, destination, &(dst.sin_addr));
inet_pton(AF_INET, source, &(src.sin_addr));
protocol= getprotobyname("ICMP");
printf("Protocol number for ICMP is %d\n",protocol->p_proto);
pingSocket= socket(AF_INET, SOCK_RAW, protocol->p_proto); // Create socket
if(pingSocket< 0){
perror("Error creating socket: ");
exit(3);
}
printf("Socket created with identifier %d\n", pingSocket);
packetSize= sizeof(struct iphdr) + sizeof(struct icmphdr);
packet = (char *) malloc(packetSize);
buffer = (char *) malloc(packetSize);
ip= (struct iphdr*) packet;
icmp= (struct icmphdr*) (packet+ sizeof(struct iphdr));
memset(packet, 0, packetSize);
//Fill up the IP header
ip->ihl= 5;
ip->version= 4;
ip->tos= 0;
ip->tot_len = htons(packetSize);
ip->id = htons(0);
ip->frag_off= 0;
ip->ttl = 255;
ip->protocol= protocol->p_proto;
ip->saddr= src.sin_addr.s_addr;
ip->daddr= dst.sin_addr.s_addr;
setsockopt(pingSocket, protocol->p_proto, IP_HDRINCL, &optval, sizeof(int)); //HDRINCL to tell the kernel that the header is already included
icmp->type= ICMP_ECHO;
icmp->code= 0;
icmp->un.echo.id= rand();
icmp->un.echo.sequence= rand();
icmp->checksum= 0;
icmp->checksum = csum((unsigned short *)icmp, sizeof(struct icmphdr));
dst.sin_family= AF_INET;
sendto(pingSocket, packet, packetSize, 0, (struct sockaddr *)&dst, sizeof(dst));
printf("Sent ping request with size %d to %s\n", ip->tot_len, destination);
printf("Request's IP ID: %d and IP TTL: %d\n", ip->id, ip->ttl);
printf("Packet: \n%s\n", packet);
// Wait for response
if( (recvSize= recvfrom(pingSocket, buffer, sizeof(struct iphdr)+sizeof(struct icmphdr), 0, (struct sockaddr *)&dst, &addrlen)) < 0){
perror("Receive error: ");
}
else{
printf("Received a reply from %s of size %d\n", destination, recvSize);
ip_reply= (struct iphdr*) buffer;
printf("Reply's IP ID: %d and IP TTL: %d\n", ip_reply->id, ip_reply->ttl);
}
free(packet);
free(buffer);
close(pingSocket);
return 0;
}
unsigned short csum(unsigned short *ptr, int nbytes)
{
register long sum;
u_short oddbyte;
register u_short answer;
sum = 0;
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1) {
oddbyte = 0;
*((u_char *) & oddbyte) = *(u_char *) ptr;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}
char* getip()
{
char buffer[256];
struct hostent* h;
gethostname(buffer, 256);
h = gethostbyname(buffer);
return inet_ntoa(*(struct in_addr *)h->h_addr);
}
I read somewhere that someone fixed this issue by setting the ICMP checksum to Zero first and then calculating it. This however doesn't work for me. Please help me out :)

How to use expat-parser with Russian charsets?

I tried to use expat for XML parsing, and I have Russian symbols in XML file, this symbols incorrectly interpret by expat.
I got expired_str: Русский текст
Instead of: Русский текст
Here is my cutted code:
static int xmlParseStartup(char *buffer, int n, START_UP_T *startUp_sp)
{
void *buff;
XML_Parser parser_p = XML_ParserCreate("UTF-8");
if (!parser_p)
{
DEBUG("Unable to create parser!\n");
return RES_ERR;
}
XML_SetUserData(parser_p, (void *)startUp_sp);
XML_SetElementHandler(parser_p, startElement, endElement);
buff = XML_GetBuffer(parser_p, n);
memcpy(buff, buffer, n);
if (XML_STATUS_ERROR == XML_ParseBuffer(parser_p, n, TRUE))
{
DEBUG("%s at line %" XML_FMT_INT_MOD "u\n",
XML_ErrorString(XML_GetErrorCode(parser_p)),
XML_GetCurrentLineNumber(parser_p));
return RES_ERR;
}
return RES_OK;
}
static void XMLCALL startElement(void *userData,
const char *name,
const char **atts)
{
int i;
START_UP_T *startUp_sp = (START_UP_T *)userData;
for (i = 0; i < startUp_sp->depthPtr; i++)
{
fprintf(stderr, ".");
}
DEBUG("[%d]name: %s\n", startUp_sp->depthPtr, name);
if (0 == strcmp(name, "response"))
{
if (0 == strcmp(atts[i], "result"))
{
startUp_sp->result = atoi(atts[3]);
DEBUG("RESULT: %d\n", startUp_sp->result);
}
else if (0 == strcmp(atts[i], "status_str"))
{
strcpy(startUp_sp->expired_str, atts[3]);
DEBUG("EXPIRED_STR: %s\n", startUp_sp->expired_str);
}
else if (0 == strcmp(atts[i], "status_width"))
{
startUp_sp->status_width = atoi(atts[3]);
}
}
startUp_sp->depthPtr += 1;
}
static void XMLCALL endElement(void *userData,
const char *name)
{
START_UP_T *startUp_sp = (START_UP_T *)userData;
startUp_sp->depthPtr -= 1;
}
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<startup>
<response name="result" value="0"/>
<response name="status_str" value="Русский текст"/>
<response name="status_width" value="120"/>
</startup>
You get a cp1251 representation of the UTF-8 char*-typed string, so the expat actually works fine - it is the console output you're having problems with.
If it is not the case, check for the utf8 marker at the beginning of the xml file (239, 187, 191 bytes in ASCII codes, or "п>ї" without quotes in CP-1251).
One more: You should check the actual encoding of the .xml file, looks like it is not what you think it is (utf-8). What editor do you use to create the file ?
The CP1251 representation of UTF-8 "Русский текст" string is "Р С_С_С_РєРёР№ С'РчРєС_С'".

LSP packet modify

anybody care to share some insights on how to use LSP for packet modifying ?
I am using the non IFS subtype and I can see how (pseudo?) packets first enter WSPRecv. But how do I modify them ? My inquiry is about one single HTTP response that causes WSPRecv to be called 3 times :((. I need to modify several parts of this response, but since it comes in 3 slices, it is pretty hard to modify it accordingly. And, maybe on other machines or under different conditions (such as high traffic) there would only be one sole WSPRecv call, or maybe 10 calls. What is the best way to work arround this (please no NDIS :D), and how to properly change the buffer (lpBuffers->buf) by increasing it ?
int WSPAPI
WSPRecv(
SOCKET s,
LPWSABUF lpBuffers,
DWORD dwBufferCount,
LPDWORD lpNumberOfBytesRecvd,
LPDWORD lpFlags,
LPWSAOVERLAPPED lpOverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
LPWSATHREADID lpThreadId,
LPINT lpErrno
)
{
LPWSAOVERLAPPEDPLUS ProviderOverlapped = NULL;
SOCK_INFO *SocketContext = NULL;
int ret = SOCKET_ERROR;
*lpErrno = NO_ERROR;
//
// Find our provider socket corresponding to this one
//
SocketContext = FindAndRefSocketContext(s, lpErrno);
if ( NULL == SocketContext )
{
dbgprint( "WSPRecv: FindAndRefSocketContext failed!" );
goto cleanup;
}
//
// Check for overlapped I/O
//
if ( NULL != lpOverlapped )
{
/*bla bla .. not interesting in my case*/
}
else
{
ASSERT( SocketContext->Provider->NextProcTable.lpWSPRecv );
SetBlockingProvider(SocketContext->Provider);
ret = SocketContext->Provider->NextProcTable.lpWSPRecv(
SocketContext->ProviderSocket,
lpBuffers,
dwBufferCount,
lpNumberOfBytesRecvd,
lpFlags,
lpOverlapped,
lpCompletionRoutine,
lpThreadId,
lpErrno);
SetBlockingProvider(NULL);
//is this the place to modify packet length and contents ?
if (strstr(lpBuffers->buf, "var mapObj = null;"))
{
int nLen = strlen(lpBuffers->buf) + 200;
/*CHAR *szNewBuf = new CHAR[];
CHAR *pIndex;
pIndex = strstr(lpBuffers->buf, "var mapObj = null;");
nLen = strlen(strncpy(szNewBuf, lpBuffers->buf, (pIndex - lpBuffers->buf) * sizeof (CHAR)));
nLen = strlen(strncpy(szNewBuf + nLen * sizeof(CHAR), "var com = null;\r\n", 17 * sizeof(CHAR)));
pIndex += 18 * sizeof(CHAR);
nLen = strlen(strncpy(szNewBuf + nLen * sizeof(CHAR), pIndex, 1330 * sizeof (CHAR)));
nLen = strlen(strncpy(szNewBuf + nLen * sizeof(CHAR), "if (com == null)\r\n" \
"com = new ActiveXObject(\"InterCommJS.Gateway\");\r\n" \
"com.lat = latitude;\r\n" \
"com.lon = longitude;\r\n}", 111 * sizeof (CHAR)));
pIndex = strstr(szNewBuf, "Content-Length:");
pIndex += 16 * sizeof(CHAR);
strncpy(pIndex, "1465", 4 * sizeof(CHAR));
lpBuffers->buf = szNewBuf;
lpBuffers->len += 128;*/
}
if ( SOCKET_ERROR != ret )
{
SocketContext->BytesRecv += *lpNumberOfBytesRecvd;
}
}
cleanup:
if ( NULL != SocketContext )
DerefSocketContext( SocketContext, lpErrno );
return ret;
}
Thank you
my comment worked out. http response headers / request turned out to end in \r\n\r\n.