|
I have a Byte buffer that I'm receving data into from an CAsyncSocket call (Receive)
BOOL bRetCode;<br />
DWORD dwBytesToRead;<br />
<br />
bRetCode = m_sConnectSocket.IOCtl(FIONREAD, &dwBytesToRead);<br />
<br />
BYTE* RecvBuffer = new BYTE[(int)dwBytesToRead];<br />
<br />
m_sConnectSocket.Receive(RecvBuffer, sizeof(RecvBuffer));<br />
<br />
onParseData(RecvBuffer, sizeof(RecvBuffer));<br />
In my onParseData function I'm trying to parse out the data. I know that certain parts of the data stream are a fixed length. So I created a structure.
struct ParseData{<br />
BYTE STAP[4]; <br />
BYTE BlockLen;<br />
BYTE HDRLen;<br />
BYTE FunctionCode;<br />
BYTE ValField;<br />
BYTE DataLen;<br />
BYTE MsgStatus;<br />
BYTE Dest;<br />
char* DataPacket;<br />
BYTE STOP[4];<br />
};<br />
ParseData myStuff;<br />
memcpy(&myStuff, dataBuffer, sizeof(myStuff));<br />
My problem is that I feel that this is not going to work and that I get an exception error because of the char* DataPacket; .
And I'd love to make my sturcture like this:
struct ParseData{<br />
BYTE STAP[4]; <br />
int BlockLen;<br />
int HDRLen;<br />
int FunctionCode;<br />
int ValField;<br />
int DataLen;<br />
int MsgStatus;<br />
int Dest;<br />
char* DataPacket;<br />
BYTE STOP[4];<br />
};<br />
I just want some direction in to where I need to go to find out:
1. How do I move this BYTE array into this structure?
2. Is there a simple way to just pull out certain parts of the data stream and plop it into the structure? example...move the first 4 bytes to the STAP the second 4 to the BlockLen...etc.
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
The problem is that a char * is the address where an array of char's can be found. In unpacking your data, you need to work out how long the string is, call new on the char * in the struct, and then assign the value. If I were you, I'd put a std::string in the struct instead, that way you don't need to clean up memory.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Christian
Thanks replying so quickly. So here is the embarassing part. My main programming language is COBOL. So this structure thing is new to me. When you say to put a std::string in the struct.....what do you mean by that?
I do understand what you are saying that the char* needs to be initialized with the length that it needs to be....and the DataLen is the size of this array....but, even if I know this amount......will me doing a memcopy easily convert the data stream to the appropriate typedef? From a BYTE to an int or a char?
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Tom Wright wrote:
So this structure thing is new to me.
A structure is just a class, under the hood the only difference is that members of a struct are public by default, members of a class, private by default.
Tom Wright wrote:
When you say to put a std::string in the struct.....what do you mean by that?
The problem with allocating a pointer is that you need to clean the memory afterwards. A std::string is probably overkill, a struct (being a class) can have a destructor, which can clean the memory for you. Something like this:
struct MyStruct
{
char * theString;
MyStruct()
{
theString = NULL;
}
~MyStruct()
{
if (theString)
delete [] theString;
}
}
I'm a long way from the last time I used C++, so there may be some syntax problems there.
A std::string is a class offered by the standard C++ library, which allows you to manipulate strings. Overkill in this case, especially if you're building a lot of them. To use it, you would put #include<string> at the top of your .cpp file, and then using std::string; somewhere underneath.
Tom Wright wrote:
will me doing a memcopy easily convert the data stream to the appropriate typedef? From a BYTE to an int or a char?
A byte is a char. So yes, so long as your string is NULL terminated ( ends with a 0 ), you can just call new on the char * to allocate memory, then do a memcpy across to it. If it's not null terminated, just add one to the size of the array you create, call memset on it to null it all, then copy the string in.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
What I did when I faced a similar problem is this:
#pragma pack(1) // This is important, otherwise your compiler might allign things wrong
struct ParseDataStruct{
BYTE STAP[4];
int BlockLen;
int HDRLen;
int FunctionCode;
int ValField;
int DataLen;
int MsgStatus;
int Dest;
char DataPacket[MaxSize+4]; // inlucdes the 4 STOP bytes
};
#pragma pack()
class ParseData {
protected:
ParseDataStruct Data;
public:
ParseData(BYTE * DataIn) { // constructor, pass a pointer to the data
ParseDataStruct *in = (ParseDataStruct *)DataIn; // my compiler is old and doesn't support the new C++ casts well, you should use them instead
memcpy(DataIn,&Data,in->BlockLen); // check the length of this copy
}
enum msgStats { ...}
enum FunctCode { ... }
msgStatus getMsgStatus();
FunctCode getFunctionCode();
char * getDataPacket();
...
}
That is create a class to wrap the whole thing. This class knows who things are represented. It can even pull the STOP bytes out if you need them, even though the data is variable length.
I'm sure there is a different way, and it might even be better. Thats just what I've done.
|
|
|
|
|
I'm trying to let the user change a dialog's font at runtime. It keeps crashing on me in "wingdi.cpp". this is what I have so far:
memset(&m_Font, 0, sizeof(LOGFONT));
CFont* pFont = this->GetFont();
pFont->GetLogFont(&m_Font);
CFontDialog dlg(&m_Font);
dlg.m_cf.Flags |= CF_TTONLY;
if(dlg.DoModal() == IDOK)
{
dlg.GetCurrentFont(&m_Font);
pFont->CreateFont(m_Font.lfHeight,m_Font.lfWidth,m_Font.lfEscapement,m_Font.lfOrientation,m_Font.lfWeight,m_Font.lfItalic,m_Font.lfUnderline,m_Font.lfStrikeOut,m_Font.lfCharSet,m_Font.lfOutPrecision,m_Font.lfClipPrecision,m_Font.lfQuality,m_Font.lfPitchAndFamily,m_Font.lfFaceName);
this->SetFont(pFont);
}
Note: at first I had pFont->CreateFontIndirect(&m_Font) but it crashed in the same place.
[insert witty comment here]
bdiamond
|
|
|
|
|
bdiamond wrote:
CFont* pFont = this->GetFont();
bdiamond wrote:
pFont->CreateFont(
There's the problem. You are trying to create a font on an object that already has a valid font created. You'll have to create a class member of type CFont , delete the font (DeleteObject() ) when you're finished with it, and recreate it with the new font information.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
I didn't get my e-mail notification from this thread so I thought nobody had answered it. I've gotten the font to change for the edit control, but now all the other controls on my dialog also change their fonts. Not to the font I selected, but to some ugly bold default system font. It's as if I have no font selected. Instead of initializing the CFontDialog with my control's current font, I used the dialog's just to see what it was. The first time, the right font is selected when I open the dialog. (10pt Tahoma) after I set the controls font once and open it again with the dialog's current font, nothing is selected as if my dialog has no font. this is what I have now:
ZeroMemory(&m_Font,sizeof(LOGFONT));
CFont* pFont = m_LockEdit.GetFont();
pFont->GetLogFont(&m_Font);
CFontDialog dlg(&m_Font);
dlg.m_cf.Flags |= CF_TTONLY;
if(dlg.DoModal() == IDOK)
{
LOGFONT lgfnt;
dlg.GetCurrentFont(&lgfnt);
m_cFont.DeleteObject();
m_cFont.CreateFontIndirect( &lgfnt );
m_LockEdit.SetFont( &m_cFont, TRUE );
pFont->DeleteObject();
}
[insert witty comment here]
bdiamond
|
|
|
|
|
bdiamond wrote:
pFont->DeleteObject();
You shouldn't delete the font that is still selected as the dialog box font, otherwise it will look like there's no font selected (and use the default system font). When you used GetFont() , you got a pointer to the current font, not a copy of the font, so if you delete it, you delete the font that the dialog box is still using. Other than this line, your code should work properly.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Hi there,
How do I add a help (?) button to the title bar of a child MDI window? Also, how can I remove the minimize and maximize buttons leaving only the help and close button just like in a dialog box.
Thanks!
|
|
|
|
|
Hello,
I'm working on a parser class. This class is recursively parsing the data, which means you can have nested elements (like an XML file).
Here is some of the code:
<br />
class CElement<br />
{<br />
private:<br />
std::list<CElement> m_lstChildren;<br />
};<br />
This code wont compile because of the following error:
<include dir> : error C2079: '_Value' uses undefined class 'CElement'
<project dir> : see reference to class template instantiation 'std::list<class CElement,class std::allocator<class CElement> >' being compiled
I guess that the class is not completely know to the compiler at the time it wants to compile the template. So I added a prototype declaration at the beginning of the header file, no luck.
I searched some FAQ's, no answer found. I hope that someone can help me out here.
Is it even possible what I'm trying to achieve?
Thanks in advance.
Multiply it by infinity and take it beyond eternity and you'll still have no idea about what I'm talking about.
|
|
|
|
|
A class can't contain an instance of itself, because that would result in infinite recursion. Or, in your case, sizeof(CElement) isn't known at the point where you refer to it in the list template parameter. You can use a std::list<CElement*> though.
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
"How does one know if one's aura is dirty? Does someone come along with their finger and write 'wash me' on it?"
-- Buffy
"Linux is good. It can do no wrong. It is open source so must be right. It has penguins. I want to eat your brain."
-- Paul Watson, Linux Zombie
|
|
|
|
|
Thanks, this is what I was afraid of. Anyway, it'll have to do...
Multiply it by infinity and take it beyond eternity and you'll still have no idea about what I'm talking about.
|
|
|
|
|
Hello guys,
someone is trying to intercept some dialog box of my application to modify its behaviour once the application is running and the dialog box opened (he doesn't have the source code of the application).
I need to prevent him to do this, could someone give me some hint on how to do it?
Thanx,
Fabio
|
|
|
|
|
This code is probably injecting a dll into your process. There are a number of ways they could be doing this.
You could try this.
Once you have built your app open the exe in Dependancy Viewer. Get a list of all the DLL's referenced by your app.
In your code you can use EnumProcessModules() and GetModuleFileName() to see what dlls have been loaded into your process. You can then determine if there is a dll loaded that you dont recognise. Im not sure if you can unload the problem dll by calling FreeLibrary() or not.
If I was in your situation I would try to get a copy of the software that is causing the problem and attempt to reverse engineer it to the point where you understand what it is doing. Then you will have a better chance of designing code to defeat it.
|
|
|
|
|
Thanx Josh...
I think they are hooking on my EXE only at a certain moment, and not since the beginning.
Is it possible to intercept the OnInitDialog message of a dialog box with an ActiveX and take the control over it? I think this is the type of attack... And in this case the dll is not part of my process, right?
About trying to get the soft causing the problem: impossible. I just have to guess and do the strongest protection possible.
I'm gonna try a sample attack program to test your protection method.
Thanx
Fabio
|
|
|
|
|
Hello,
Can someone give me some explanations how C++ actually doing the type casting? Is a Dynamic Cast really expensive? What about Static Cast, Constant Cast and Interrupter Cast? Thanks!
Nachi
|
|
|
|
|
The authoratative source on all things performance in C++ is:
C++ Standards Committee Performance TR[^]
From the performance TR:
const_cast<Type>(expression) // §IS-5.2.11
static_cast<Type>(expression) // §IS-5.2.9
reinterpret_cast<Type>(expression) // §IS-5.2.10
dynamic_cast<Type>(expression) // §IS-5.2.7
The first three forms of type conversion operator have no size or speed penalty versus the equivalent cast notation. Indeed, it is typical for a compiler to transform cast notation into one of the other type conversion operators when generating object code. However, dynamic_cast<t> may incur some overhead at run-time if the required conversion involves using RTTI mechanisms such as cross-casting (§2.3.8).
The report goes on to give examples of how dynamic cast costs are dependant on the types you are casting to and from. If you believe casting to be an issue profile your application and compare the cost of the cast to the rest of the application.
Casting is not likely to be significant performance cost in the bigger picture unless you do it very often - in which case the design may require refactoring. There are however, two conflicting opinions on the use of dynamic casts. One is that it is best to avoid dynamic casting where possible through the use of the visitor pattern (Design Patterns - Gamma, Helm et al.), and the other is that the use of dynamic_casts elminates the need for large ammounts of code that the visitor pattern requires.
Nish's Excellant article on Casting Basics[^]
If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts your aim;
Yours is the Earth and everything that's in it.
Rudyard Kipling
|
|
|
|
|
Hi Everybody,
How r u all. I would like to know is there any way to search and Image inside a bitmap. If you kow please tell me any effective way or algorithm you follow.
Thanks in Advance.
If you have faith in cause and
the means and in gos, the hot
Sun will be cool for you.
|
|
|
|
|
If you know the byte-pattern (e.g., colors, palettes) that the image is comprised of, I don't see why not.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
I think he didn't mean an easy computer-generated bitmap. Image detection is not easy. There are very complicated math algorithms about that. I've read a book about that.. although it has been a long time ago I think there was a way doing it by using the FFT algorithm in a special way, but I'm not sure about that anymore. You could google for "digital image processing".
Don't try it, just do it!
|
|
|
|
|
I need help. I want to extract the ISO file to the local harddrive. Is there an sample code or some sample written in VC++ 6.0 or C#, which I can take a look. Please help.
Thank You
Regards,
ntuyen01
|
|
|
|
|
ntuyen01 wrote:
...the ISO file...
Ok, get me up to speed here. What is the ISO file?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
DavidCrow wrote:
What is the iso file?
Bun of files compress to a single file to make the CD image.
Example: testing.iso
If someone gave me an testing.iso from the CD want to extract it out to the local disk.
Thank You
Regards,
ntuyen01
|
|
|
|
|
Does knowing the format of an ISO file help?
http://www.wotsit.org/download.asp?f=iso9660
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|