|
You have to understand that an LPSTR is a very primitive thing: a pointer to an array of characters that is supposed to have a \0 on the end. You can do operations with LPSTR (look at the strcat function) but you have to make sure that you have allocated another array to hold the result that is large enough. Manipulating strings this way has been done for years in C and has led to innumerable buffer overflow errors. You should never do it in C++. You should use the string classes (std::string, std::wstring for wide characters) that are in the standard library, which take care of buffer allocation (and many other useful things) for you. You can concatenate std::string with the overloaded + operator and get just what you expect.
|
|
|
|
|
this seemed to work:
#include <iostream.h>
#include <string.h>
#include <windows.h>
LPSTR concat(LPSTR s1, LPSTR s2)
{
char* str = new char[strlen(s1) + strlen(s2) + 1];
LPSTR final_str = str;
strcpy(str, s1);
strcat(str, s2);
return final_str;
}
int main()
{
LPSTR s1 = "this is a";
LPSTR s2 = " test.";
cout << concat(s1, s2) << endl;
return 0;
}
|
|
|
|
|
Quick answer: C-style strings don't work like that.
Long answer: See the above replies for some code that does what you want, although you must learn how C-style strings work in order to use the strcat /strcpy functions properly. The std::string class is much easier to learn and use if you don't absolutely need a C-style string.
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
Actual sign at the laundromat I go to: "No tinting or dying."
|
|
|
|
|
it is wrong certainly.because your "myString" points to a static data area,which can not be changed.so ,your "myString = "Whats " + myString" is wrong.you must do so:
char str[10]="up?";
char mystring[20];
sprintf(mystring,"Whats %s",str);
|
|
|
|
|
Hi all,
I have an app that must implement window (f1) and context level (shift+f1) help. Conext level help is a problem.
There does not seem to be any methods available to so that i can be nofified when shift+f1 is pressed . If none are available then the only solution is to add an entry in the accelerator table. However, then I need someway to get a handle to the currently focused component (so i know what context help to show). I am not sure how to do this either!
any help appreciated...
brian.
|
|
|
|
|
Two questions here. Question the first: I have a dialog app and on it I have a group box with a radio button over it's heading. The reason I'm doing this is because I want it to enable the controls in the groupbox when the radio is pressed, and I'd like it to look like the radio and groupbox are one control. The problem I'm encoutering is that the radio always appears "behind" the groupbox so there is a line through it. When the radio is clicked it's drawn properly so the first thing I tried doing is setting the focus to it when the dialog was enabled, that didn't really work. So my question is how would I get the radio to show on top of the groupbox?
Question the second: I saw a post on here a couple days ago with what appeared to be a similar question and the person was directed to look at WS_CLIPSIBLINGS so I did that as well. It seems like that's what I want so I tried this code to change the style of that radio button:
HWND hStyle = GetDlgItem (IDC_CLOSED_IN_RANGE_CLEC)->GetSafeHwnd ();
LONG lStyle = ::GetWindowLong (hStyle, GWL_STYLE);
lStyle |= WS_CLIPSIBLINGS;
::SetWindowLong (hStyle, GWL_STYLE, lStyle); It didn't work so really my second question is is WS_CLIPSIBLINGS the right thing for what I want and is that code above even close to how the style of a window should be changed?
Any and all help is appreciated.
- Aaron
|
|
|
|
|
1. Do you mean a radio button, or a checkbox?
2. Probably, yes. That code should do what you want, assuming that the IDC is that of your radio button.
In the tab order, is the radio button earlier(lower) or later(higher) than the group box?
(in VC++6 IDE, use ctl-D to display tab order numbers)
If all else fails, synthesis a groupbox with lines. It's an old trick, but it might just work....
Steve S
|
|
|
|
|
I mean radio button (it makes sense in the context of the program).
I'm pretty sure that's the IDC of the button, but I'll double check. I'll also check the tab order, if it was earlier(lower) should it be on top?
I'd prefer not to "synthesis" the groupbox, but thanks for the tip if nothing else works.
- Aaron
|
|
|
|
|
|
I'll check those out. Thanks.
- Aaron
|
|
|
|
|
|
Hello there,
I wrote some applications and since a lot of these applications use common classes I figured I`ld put the common classes and code in a dll and load the dll in the apps. That way my executables would be smaller and my projects would have a better overview (less files).
The following I noticed however:
when including a certain class in my dll that required me to link to a lib the dll file increased with 150kB, but including the class in my app and linking to the lib increased the exe with only 80kB.
How does it come that the same code (except for dllexport) results in so much more memory in a dll compared to a exe?
Thanks to anyone that might shed some light on this...
|
|
|
|
|
When you are making an EXE, the linker knows your one exported function main is the top of
the dependency tree. If you don't call a function, and none of the functions you call call the
function (even indirectly), then it is thrown away. This includes functions in the EXE you wrote.
When you write a DLL, you have a lot of "tree tops", all of which may be called by another EXE / DLL.
So the linker cannot cull dead code, as it may be called.
Did that make sense to you?
Iain.
|
|
|
|
|
Yeah I did to me
MSN Messenger.
prakashnadar@msn.com
|
|
|
|
|
It makes sense.. Thanks for the answer, I`ll just have to look now when to put something in the dll and when it might not be the best solution... Because size matters in my apps.
thx again...
|
|
|
|
|
Hai friends
I have problem while GIF decoding . What i do when a code is
coming which not in translation table ?. Can i output previous code or current code ? . what will i add to the translation table ?.
Please Help me
Thanks in Advance
by Krishandevan
krishnadevan@ushusteh.com
If anu useful site found please inform me friends
Its Urgent
krishnadk
|
|
|
|
|
I want to write a program with MFC to read and write to Ms Excel and Access.
How can I do it?
Mehdi Hosseinpour
|
|
|
|
|
Search MSDN for articles on Excel (OLE) Automation.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Hi all
i use the following piece of code to call an external program, however
i keep getting a warning message returned back which i compile
im using VS2003
the code is
HINSTANCE ok = ShellExecute(NULL, "open", "Update.exe", "NewUpdate", NULL, SW_SHOW);
if ((int)ok > 32)
{
AddToLog("Program run sucessfully",0);
}
else
{
AddToLog("Program failed to run ",0);
}
and the warning message i get back is
(1467): warning C4311: 'type cast' : pointer truncation from 'HINSTANCE' to 'int'
does ne1 have any ideas ??
thanks
si
|
|
|
|
|
si_69 wrote:
if ((int)ok > 32)
nothing to worry about HINSTANCE is 32 bit data while
int is 16 bit data so when you type cast from higher bit size data to lowerbitsize data you tend to loose some data (16 high bits of information)
Logic is perfectly fine in this case and will work without any problem.
If you are still worried about the warning then
put long instead of int.
like this
if ((long)ok > 32)
MSN Messenger.
prakashnadar@msn.com
|
|
|
|
|
Int is 32 bits in all versions of WIN32 Visual C++.
John
|
|
|
|
|
Yeah you are right, he got a warning for trancation of data type, so obviously there was some size difference between the two.
MSN Messenger.
prakashnadar@msn.com
|
|
|
|
|
According to the MSDN documentation, the HINSTANCE return value is not a true HINSTANCE type, but used for compatibility reasons.
So I assume it is safe to ignore the cast warning to an int (or you may suppress the warning with a #pragma, if it bothers you).
msdn ref[^]
I Dream of Absolute Zero
|
|
|
|
|
si_69 wrote:
if ((int)ok > 32)
It's not good practice to use hard-wired numbers in your code. I suggest using HINSTANCE_ERROR instead. Then you can do something like:
HINSTANCE hResult = ShellExecute(...);
if (hResult < (HINSTANCE) HINSTANCE_ERROR)
AddToLog("Program failed to run", 0);
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
I'm trying to access function in a class which is defined in another class. I've tried to declare it as a friend, however this won't work for me.
Suggestions much appreciated
Kind Regards
Caoimh
|
|
|
|