|
You could do it either way. If you know the structure of the data then a struct can be easier.
For example, following your proposed "packet header" which has an id and a length, you could make
a struct like this:
#pragma pack( push, PACKETHEADERpack, 1 )
struct PACKETHEADER
{
unsigned char Id;;
unsigned short Length; <code>
};
#pragma pack( pop, PACKETHEADERpack)
Note that the structure packing is set to 1 so no padding is added to the structure or its
members.
You can then receive a header on a socket something like:
PACKETHEADER PacketHdr;
recv(socket, (char *)&PacketHdr, sizeof(PACKETHEADER), 0);
You also may need to consider byte ordering in multi-byte numeric variables. If you will be
communicating between different platforms then you may need to convert them to network byte
order, send them, then convert them to the native format on the receiving end.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi Mark
thank you for your help. It was very useful![Smile | :)](https://www.codeproject.com/script/Forums/Images/smiley_smile.gif)
|
|
|
|
|
I have a DLL which is being developed in VC6.
This DLL uses internal resources such as a look-up table which i access using
AfxGetInstanceHandle() , FindResource and LoadResource API's
This works as expected.
However I have a project configuration that also creates a static library for the same , in which case the above mentioned API's will not be able to access the internal resource.AfxGetInstanceHandle will provide the handle to the application that uses this static library. With this condition , it seems that the internal resource needs to move to the application that uses the static library. For security reasons I dont want to expose this resource to external applications . Is it possible by some means to access the resource from the static library without having to move the internal resource to the application ?
Any ideas or suggestions are desperately needed .
Thanks
|
|
|
|
|
act_x wrote: Is it possible by some means to access the resource from the static library without having to move the internal resource to the application ?
Try this at the beginning of the function that accesses the resource:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
New to VC++, working with 6.0
Trying to read a text file one character at a time. If program finds a comma in text file, it stops and creates a string out of all the preceding characters. No problem reading the file or recognizing the comma. The program won't concatenate the chars into one string.
Here's the code:
char buffer[1];
char cur_char = NULL;
CString cur_data;
UINT bytesread;
do
{
cur_data += cur_char;
bytesread=radardata.Read(buffer,1);
cur_char=buffer[0];
}
while (cur_char != ',');
Every time I run it, cur_data ends up NULL. Here's what else I've tried with the same result:
cur_data = cur_data + CString(cur_char);
cur_data = CString(cur_data) + CString(cur_char)
Thanks in advance!
|
|
|
|
|
cur_data should end-up with NULL as you are adding NULL to it from the beginning.
The following will work;
char buffer[1];
char cur_char;
CString cur_data;
UINT bytesread = 0;
do
{
bytesread += radardata.Read(buffer,1);
cur_char=buffer[0];
cur_data += cur_char;
}
while (cur_char != ',');
--
=====
Arman
|
|
|
|
|
|
pkoelsch wrote: Trying to read a text file one character at a time.
You do realize how inefficient this is, don't you?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Like I said, I'm new to VC++. No classes, just opened a book. The last time I did any formal computer programming, we still used line numbers. I'm open to any recommendations.
|
|
|
|
|
Hi All,
I always appreciate your help.
I have a problem with ifstream
This is my 'input.txt' file.
The first digit of each line is the length.
How can I read in this file using while() or for()
2 3 4
5 7 8 10 11 12
7 8 10 16 17 54 37 38
3 33 51 48
...
I tried this one.
...
ifstream inpf_EX("input.txt");
for (i=1; i<=N_Of_LINE;i++)
{
inpf_EX>>EXIST[i][0]);
len=EXIST[i][0];
for(j=1; j<=len; j++)
{
inpf_EX>>EXIST[i][j]);
}
}
Thanks!
...
Yonggoo
|
|
|
|
|
Yonggoo wrote: How can I read in this file using while() or for()
Neither are necessary, and their use would only serve to confuse you further. You can use an iterator on a stringstream object, and then use the copy() function. You don't need an extra digit in the front to tell how many items follow. If it has to stay, however, just read it in and ignore it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I have not used an iterator on a stringstream.
Does std c++ have iterator?
Thanks!
Yonggoo
|
|
|
|
|
Yonggoo wrote: Does std c++ have iterator?
Yes.
class LineItem
{
public:
LineItem() {}
~LineItem() {}
void setLength(const int length) { _Length = length; }
int getLength() const { return _Length; }
void setNumbers(const std::vector<int>& numbers) { _Numbers.assign(numbers.begin(), numbers.end()); }
std::vector<int> getNumbers() const { return _Numbers; }
private:
int _Length;
std::vector<int> _Numbers;
};
std::ostream& operator<<(std::ostream& os, const LineItem& s)
{
os << s.getLength() << "|";
const std::vector<int> numbers = s.getNumbers();
std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}
std::istream& operator>>(std::istream& is, LineItem& s)
{
int length;
is >> length;
std::string sNumbers = "";
std::getline(is, sNumbers);
std::stringstream ss(sNumbers);
std::vector<int> numbers;
std::copy(std::istream_iterator<int>(ss), std::istream_iterator<<int>(), std::back_inserter(numbers));
s.setLength(length);
s.setNumbers(numbers);
return is;
}
void main( void )
{
std::ifstream fin;
std::vector<LineItem> lines;
fin.open("c:\\input.txt");
std::copy(std::istream_iterator<LineItem>(fin), std::istream_iterator<LineItem>(), std::back_inserter(lines));
fin.close();
std::copy(lines.begin(), lines.end(), std::ostream_iterator<LineItem>(std::cout, "\n"));
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
The following is my code for when I click on a button that's labeled "Start". I use a functor to call my class member function pointer (via Execute(), where Execute() takes an int& parameter). When I set a breakpoint at the Execute() calls, and I hover my mouse over "i", the MSVC debugger tells me that i has some really high value like 4632955, and when I check &i in the immediate window, it tells me the memory address of i. Then, I go into Execute() to call the specific function pointer &MainWindow::DoWork, when I check the input parameter (which takes an int& as mentioned earlier), the memory address of that input parameter is &i + 4 bytes. Basically, what I'm saying is that MSVC shows me different memory address values from the calling function ButtonStart() and within the called function DoWork(). The value from ButtonStart() is wrong, as my int is not some really high number, but the value that is actually received in DoWork() is right, and ends up being 5 (what I set it to in the beginning). This is the first problem and I don't know why this is happening.
The second problem is that some of my lines of execution are being skipped. Actually, that is inaccurate, as they are being executed but I cannot set a breakpoint at some of the lines such as the line int i = 5. MSVC will just shift my breakpoint down to the Execute() calls. My debugging info is turned on, and I'm am building in Debug mode, so this shouldn't happen (I've had it happen when building in Release mode).
void MainWindow::ButtonStart()
{
if (!m_threadPool && !m_source && !m_receiver)
{
m_source = new EventSource();
m_receiver = new EventReceiver();
m_threadPool = new ThreadPool(m_source, m_receiver);
m_receiver->HookThreadPool(m_source);
int i = 5;
CallbackWrapperSpecific<MainWindow, void (MainWindow::*)(int&), int> cbwsPtr(this, &MainWindow::DoWork);
CallbackWrapperBase<int>* cbwbPtr = &cbwsPtr;
cbwbPtr->Execute(i);
cbwbPtr->Execute(i);
}
}
|
|
|
|
|
Is a Rebuild All in order?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yep, I've done multiple cleans and rebuilds -- still no luck. I wonder if I've corrupted the memory somewhere... or I need to enable some kind of extra flag for debugging MFC Multithreaded applications, etc.
|
|
|
|
|
Bah, it was just optimizing for maximum speed (which was a default setting) that caused these problems... I turned it off (/Od) and now I can see everything the way I want to for a debug version.
|
|
|
|
|
Cyrilix wrote: Bah, it was just optimizing...
In debug mode?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yes, it was the default in debug mode when I created the project.
|
|
|
|
|
<>.,yuutyutyutyu,/,.//,.//,./<.<><<>,<>
';l';l
|
|
|
|
|
Will you stop spamming the forums with your crap please ?
|
|
|
|
|
Dear all
i need your help. in advance i thank u. my issue is as follows:
if i have a menuitem, after i click it, one dialog appears:ask users to enter parameters below: length=10 pixel, width=10pixel, height=10 pixel, then click ok.correspording to cube will show in window. how to program this point? who have any such snippet? so urgent. thanks very much.
regards
zhiyuan
Li Zhiyuan
6/14/2007
|
|
|
|
|
li zhiyuan wrote: who have any such snippet?
My good friend the internet [^] does.
|
|
|
|
|
I am tiered of banging my head against the wall trying to figure this out. So I am here to find out from the experts what the problem might be. The following code:
void CMonthCalCtrlEx::OnMouseMove(UINT nFlags, CPoint point) <br />
{<br />
MCHITTESTINFO mcHitTest;<br />
mcHitTest.cbSize = sizeof(MCHITTESTINFO);<br />
mcHitTest.pt = point;<br />
<br />
switch(HitTest(&mcHitTest))<br />
{<br />
case MCHT_CALENDARDATE:<br />
case MCHT_CALENDARDATENEXT:<br />
case MCHT_CALENDARDATEPREV:<br />
{<br />
if(memcmp(&mcHitTest.st, &m_stOldToolTipDate, sizeof(SYSTEMTIME)))<br />
{<br />
m_tooltip.Activate(TRUE);<br />
<br />
if(m_mapHolidayNames.Lookup(mcHitTest.st.wYear * 10000 + mcHitTest.st.wMonth * 100 + mcHitTest.st.wDay, strToolTip))<br />
{<br />
m_tooltip.Activate(TRUE);<br />
m_tooltip.UpdateTipText(strToolTip, this);<br />
<br />
MSG msg;<br />
msg.hwnd = m_hWnd;<br />
msg.message = WM_MOUSEMOVE;<br />
m_tooltip.RelayEvent(&msg);<br />
}<br />
else<br />
{<br />
ExtndedToolTip(mcHitTest);<br />
m_tooltip.UpdateTipText(strToolTip,this);<br />
MSG msg;<br />
msg.hwnd = m_hWnd;<br />
msg.message = WM_MOUSEMOVE;<br />
m_tooltip.RelayEvent(&msg);<br />
}<br />
}<br />
break;<br />
}<br />
default:<br />
{<br />
memset(&m_stOldToolTipDate, 0, sizeof m_stOldToolTipDate);<br />
m_tooltip.Activate(FALSE);<br />
break;<br />
}<br />
}<br />
CMonthCalCtrl::OnMouseMove(nFlags, point);<br />
}
worked fine under Microsoft Visual C++ 6.0. However, I have upgraded to Visual Studio C++ 2005 and now the code fails. Can anyone please point in in the right direction on how to fix it in VS? Everything compiles ok, but it seems to by pass this MCHITTESTINFO mcHitTest; and does not assign the point value to mcHitTest.pt = point;. So do I have to use some other method for dealing with OnMouseMove in VS? Any help would be appreciated.
Thanks,
Charlie
Everything is Free... Until You Have to Pay for it...
Platforms Windows 2000/XP Professional using Visual C++ 6.0
|
|
|
|
|
I'm not sure if this is the problem, but the point passed to OnMouseMove is in client coordinates.
Is the MCM_HITTEST (HitTest()) message expecting screen coordinates?
Never mind - I tested your code and it worked for me...
Mark
-- modified at 13:39 Wednesday 13th June, 2007
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|