|
How to convert a PLBYTE to integer array?
Thx!
|
|
|
|
|
int* makeIntArrayFromByteArrary(LPBYTE *pByte,size_t cbByte)
{
int* pInt = (int*)malloc(cbByte * sizeof(int));
if( pInt ) {
int i;
for( i=0; i<cbByte; ++i )
pInt[i] = pByte[i];
}
return pInt;
}
int* makeIntArrayFromByteArrary(LPBYTE *pByte,size_t cbByte)
{
int* pInt = new int[cbByte];
if( pInt ) {
for( int i=0; i<cbByte; ++i )
pInt[i] = int(pByte[i]);
}
return pInt;
} Do not forget, you are responsible for freeing memory after you are finished using the allocated arrary of intergers.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Maybe it is really a newbie problem, but how to obtain the size of *pByte? just use sizeof operator?
|
|
|
|
|
samhwang wrote:
...how to obtain the size of *pByte? just use sizeof operator?
Pointers have a constant size of 4 bytes. Perhaps you are wanting to know how many bytes the pointer is pointing to.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Sorry, I've been away!
1) "PLBYTE" is not a defined type it should be "LPBYTE" or "BYTE*".
2) I made a mistake: "LPBYTE *pByte" should be "LPBYTE pByte" or "BYTE* pByte".
sizeof examples:
BYTE b;
int i;
BYTE ba[10];
int ia[10];
BYTE* pba = ba;
int* pia = ia;
//sizeof(b) = sizeof(BYTE) = 1
//sizeof(i) = sizeof(int) = 4 (on 32-bit compilers)
//sizeof(ba) = 10*sizeof(ba[0] = 10*sizeof(BYTE) = 10
//sizeof(ia) = 10*sizeof(ia[0] = 10*sizeof(int) = 10*4 (on 32-bit compilers)
//sizeof(pba) = sizeof(BYTE*) = 4 (on 32-bit compilers)
//sizeof(pia) = sizeof(BYTE*) = 4 (on 32-bit compilers)
Notes:
1) sizeof(int) is the size of a machine word.
2) A machine word is the maximum number of bytes that can fit into one machine register.
3) Reality: What the size of an int is depends on the compiler you are using;
16-bit -> sizeof(int) = 2,
32-bit -> sizeof(int) = 4,
64-bit -> sizeof(int) = 8.
Well, I hope that answers all your questions.
Good Luck!
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Can someone upload amstream.h somewhere? Visual Studio 2005 doesn't have it...
|
|
|
|
|
*sigh* What do you expect to do with a single header file ?
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Why does this program crush?, what have i left?
<br />
#include "stdio.h"<br />
<br />
<br />
int main(int argc, char* argv[])<br />
{ <br />
FILE* fp;<br />
char fname[10];<br />
<br />
printf("Enter the file name: ",fname);<br />
scanf ("%s",fname);<br />
<br />
if( fp )<br />
fopen("&fname","wt");<br />
{ <br />
int i;<br />
for( i=0; i<10; ++i ) <br />
fprintf(fp,"Line %d\n",i); <br />
<br />
fclose(fp); <br />
} <br />
return 0;<br />
}
oam
|
|
|
|
|
|
mpapeo wrote:
if( fp )
fopen("&fname","wt");
{
int i;
for( i=0; i<10; ++i )
fprintf(fp,"Line %d\n",i);
fclose(fp);
}
it's should be :
fopen(&fname,"wt");
if( fp )
{
int i;
for( i=0; i<10; ++i )
fprintf(fp,"Line %d\n",i);
fclose(fp);
}
|
|
|
|
|
Actually, fp = fopen (...); , but you probably meant that.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
right
|
|
|
|
|
Still it crushes
oam
because i have made the changes you suggested.
<br />
#include "stdio.h"<br />
<br />
<br />
int main(int argc, char* argv[])<br />
{ <br />
FILE* fp;<br />
char fname[30];<br />
<br />
printf("Enter the file name: ",fname);<br />
scanf ("%s",fname);<br />
<br />
<br />
fopen(&fname,"wt");<br />
if( fp )<br />
{ <br />
int i;<br />
for( i=0; i<10; ++i ) <br />
fprintf(fp,"Line %d\n",i); <br />
<br />
fclose(fp); <br />
} <br />
return 0;<br />
}<br />
|
|
|
|
|
you still have yet to assign fp as was stated above
|
|
|
|
|
Well it works now.
Thanks
oam
|
|
|
|
|
mpapeo wrote:
if( fp )
fopen("&fname","wt");
{
Should be:
fp = fopen(fname, "wt");
if (NULL != fp)
{
...
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
|
Is there a way to determine if a socket that is connected to my CSocket has been closed (without overriding onClose function in class derived from CSocket)?
My function:
CString Connection::read()
{
CString out;
DWORD numRead;
do
{
::Sleep(100);
socket->IOCtl(FIONREAD, &numRead);
} while(numRead == 0);
int count = socket->Receive((void*)out.GetBuffer(numRead), numRead);
out.ReleaseBuffer(numRead);
out.TrimRight();
cout << "Connection->reading: index: " << index << ", count: " << count << ", msg: " << (LPCTSTR)out << endl;
return out;
}
I'm using this function in multithread server application. Threads are waiting for new messages and processing them sometimes requires writing to other threads' sockets.
|
|
|
|
|
Hi,
Here is useful link(especialy the examples):
http://tangentsoft.net/wskfaq/[^]
Are you using P2P or broadcast or multycast connection?
I'm not using CSocket , I'm using SOCKET with all WinSock function,
and if recv() return 0 so the client closed the connection,so i guess it's the same with CSocket.
Good luck,
Eli
|
|
|
|
|
Thanks for Your response.
socket->Receive returns 0 if socket is closed, but the problem is, that I'm using socket->IOCtl to check if I can read data from socket (and not to block socket with socket->Receive, because other thread may be trying to send something to the same socket).
socket->IOCtl returns 0 when there is nothing to read (so when socket is closed it also returns 0), and code with socket->Receive won't be executed.
I'm looking for simple method to check if socket was closed by client.
Lets say I'm lazy and I don't want to rewrite my code
|
|
|
|
|
In order to use non blocking socket,try this:
unsigned long argp = 1;
ioctlsocket(YourSocket , FIONBIO , &argp);
and it's should solve your problem of blocking socket.
About your's socket->Receive()->i've searched MSDN for CSocket functions and there is no Receive() in CSocket class,so how do you use that function?
Good luck with the Non blocking socket,
Eli
|
|
|
|
|
I did as You suggested, only in CSocket style:
DWORD ctl = 1;
socket->IOCtl(FIONBIO, &ctl);
I've rewritten my code:
CString Connection::read()
{
TCHAR c;
CString out;
DWORD numRead;
do
{
::Sleep(100);
socket->IOCtl(FIONREAD, &numRead);
if(numRead == 0)
{
int ret = socket->Receive(&c, 1);
if(ret == 0) valid = FALSE;
if(ret == SOCKET_ERROR)
{
if(socket->GetLastError() == WSAECONNRESET)
valid = FALSE;
}
if(!valid) return CString();
}
} while(numRead == 0);
int count = socket->Receive((void*)out.GetBuffer(numRead), numRead);
out.ReleaseBuffer(numRead);
out.TrimRight();
cout << "Connection->reading: index: " << index << ", count: " << count << ", msg: " << (LPCTSTR)out << endl;
return out;
}
void Connection::write(CString s)
{
CSingleLock singleLock(§ion);
singleLock.Lock();
s += "\n";
if(socket->IsBlocking()) socket->CancelBlockingCall();
int count = socket->Send((void*)s.GetBuffer(10), s.GetLength());
cout << "Connection->writing: index: " << index << ", count: " << count << ", msg: " << (LPCTSTR)s;
if(count == 0)
cout << "Error: " << socket->GetLastError() << endl;
}
I'm no so sure about the line: if(socket->IsBlocking()) socket->CancelBlockingCall(); but I was forced to add this, because with two clients connected to server, writing to one of them was generating error WSAEINPROGRESS.
socket->Receive() is function of CSocket base class: CAsyncSocket.
Thanks again.
|
|
|
|
|
I want to get memory usage information of one process.
Looking at the old threads, I have been told to execute the following codes.
#include <windows.h>
#include <stdio.h>
#include "psapi.h"
void PrintMemoryInfo( DWORD processID )
{
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Print information about the memory usage of the process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL == hProcess)
return;
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
{
printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
printf( "\tPeakWorkingSetSize: 0x%08X\n",
pmc.PeakWorkingSetSize );
printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n",
pmc.QuotaPeakPagedPoolUsage );
printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
pmc.QuotaPagedPoolUsage );
printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n",
pmc.QuotaPeakNonPagedPoolUsage );
printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
pmc.QuotaNonPagedPoolUsage );
printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
printf( "\tPeakPagefileUsage: 0x%08X\n",
pmc.PeakPagefileUsage );
}
CloseHandle( hProcess );
}
void main( )
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the memory usage for each process
for ( i = 0; i < cProcesses; i++ )
PrintMemoryInfo( aProcesses[i] );
}
then create a console application, copy above code, compile and run.
my question is :
when runing the program , one condition function OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ, FALSE, processID ) return NULL, the other condition function GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) return FALSE, so I can't get anything about the memory usage information per process.
can anyone help ?
|
|
|
|
|
Are you passing a valid process ID to the function?
"Aerodynamically, the bumble bee shouldn't be able to fly, but the
bumble bee doesn't know it so it goes on flying anyway." - Mary Kay Ash
My Articles
|
|
|
|
|
Try:
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );
if (NULL == hProcess)
{
DWORD dwError = GetLastError();<big>
return;
}
...
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|