|
OK, Still having a little trouble with this. The CStr class is derived from CStrBase class. In the base a static vector is filled with the loaded string. To cut down on memory usage, I first load the string into a buffer, then use operator new to allocate the right sized memory. Trouble is I can't delete it, everywhere I try either causes the string to go out of scope or an assertation failiure.
struct _string
{
UINT id;
LPTSTR str;
_string() { str = NULL; };
_string(const _string& s)
{
id = s.id;
str = s.str;
}
};
class CStrBase
{
public:
static std::vector<_string> m_vec;
static TCHAR m_Buffer[2048];
CStrBase(){};
CStrBase(UINT id,HINSTANCE inst)
{
for ( UINT i=0; i<m_vec.size(); i++ )
if ( id == m_vec[i].id )
return;
m_Buffer[0] = 0;
int len = LoadString(inst,id,m_Buffer,2048);
_string s;
s.id = id;
s.str = new TCHAR [len+1];
_tcscpy_s(s.str,len+1,m_Buffer);
m_vec.push_back(s);
}
~CStrBase()
{
for ( UINT i=0; i<m_vec.size(); i++ )
}
};
TCHAR CStrBase::m_Buffer[2048];
std::vector<_string> CStrBase::m_vec;
class CNStr : public CStrBase
{
public:
TCHAR str[2048];
LPTSTR s;
UINT m_id;
CNStr(UINT id, HINSTANCE inst = GetModuleHandle(NULL)) : CStrBase(id,inst)
{
m_id = id;
}
operator LPCTSTR()
{
for ( UINT i=0; i<m_vec.size(); i++ )
if ( m_vec[i].id == m_id )
return m_vec[i].str;
return _T("");
}
};
|
|
|
|
|
Never mind, easy solution. I just needed to set a pointer and check it in the destructor.
|
|
|
|
|
...also, your struct needs reference counting with the copy ctor you supplied.
|
|
|
|
|
waldermort wrote: I'm getting fed up of calling LoadString() every other line, so I decided to make a wrapper class.
See here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Thanks David, that article is where I got the idea to create this class. But unfortunatly, CMsg is derived from CString and can only be used with functions expecting a CString object. His class would be perfect, but it requires the use of MFC and it doesn't cache loaded strings.
|
|
|
|
|
waldermort wrote: But unfortunatly, CMsg is derived from CString...His class would be perfect, but it requires the use of MFC...
I knew you were looking for a non-MFC solution. How much work would be involved in changing all CString references to std::string instead?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi,
I'm new to MFC and so this is probably a simple question - but I can't seem to find an answer after much looking. The background to the problem is:
1) I create a standard SDI application (using the wizard in Visual Studio).
2) In the View class (header) I create a combo box member varaible "CComboBox m_Com1; ".
3) I then call the Create function of "m_Com1 " from the "OnInitialUpdate " function of the view ("m_Com1.Create(WS_VISIBLE | CBS_DROPDOWNLIST , CRect(400,100,500,200) , this, 9); ").
This compiles fine but when I run the program and try and start a new file (or open a new file) from the main menu, I hit an assertion failure. This derives from "ASSERT(pWnd->m_hWnd == NULL); // only do once ", within "void AFXAPI AfxHookWindowCreate(CWnd* pWnd) ".
Could anyone tell me why this is happening?
Many thanks,
b_e_n_82
b_e_n_82
|
|
|
|
|
It is telling you that the window has already been created, and this is the second time you are calling Create() .
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
You can use of if before create combobox
|
|
|
|
|
Remeber that SDI applications reuse their view when you open a new document.
|
|
|
|
|
b_e_n_82 wrote: 3) I then call the Create function of "m_Com1" from the "OnInitialUpdate" function of the view...
Create the control in the view's OnCreate() method instead.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
hey every one!
i was wondering is there any way that i can be able to change the text on a message box? what i mean is, in the funtion MessageBox(NULL,"text1","text2",MB_OK); is there any way that i can change the "text1" depending on what happends in the code?
thanx, jt
|
|
|
|
|
just pass a pointer to text instead.
<br />
char *foo = "Hello";<br />
char *bar = "World";<br />
char *txt = foo;<br />
MessageBox(NULL,txt,bar,MB_OK);<br />
txt = bar;<br />
MessageBox(NULL,txt,bar,MB_OK);<br />
You can then send in any string you like just by setting up a pointer.
|
|
|
|
|
hhhmm.. ya i tryed that it works great! thanx alot now i ave a good basis i can go on.
ok quick question what about this piece of code
#include <windows.h><br />
#include <stdio.h><br />
char *foo;<br />
char *fo="yo";<br />
char *txt=foo;<br />
int main(){<br />
char input;<br />
gets(input);<br />
<br />
input=foo;<br />
<br />
MessageBox(NULL,foo,fo,MB_OK);<br />
<br />
<br />
}
this dosnt seem to work for me can u shed some light?
thanx, jt
|
|
|
|
|
the line "input=foo" overwrites the text you input with an undefined string.
I think what you meant was "foo=input"
|
|
|
|
|
benjymous wrote: I think what you meant was "foo=input"
foo = &input perhaps.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Can you more explain change text on a MessageBox do you need to change text on parameters?
|
|
|
|
|
yea kinda like that.
but i wanna be able to change parameter 2 in the message box. depending on if a certin thing happends or not. ya see?
|
|
|
|
|
Why waldermort answer doesnt work for you
|
|
|
|
|
jqt wrote: is there any way that i can change the "text1" depending on what happends in the code?
Yes, just use a variable instead of a string literal.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
hhhmmm.. ok i will try that.
thanx all for your help! (and time)
|
|
|
|
|
Hello everyone!
First of all, no, I'm not going to make a sequel to my previous program, it's for a "well-hearted" project.
But how do you get the stored login info for MSN Messenger? Thanks!
Windows Calculator told me I will die at 28.
|
|
|
|
|
I heard somewhere that later versions of VC++ are able to support a variable number of arguments, but I can no longer find the article. I have a function declared as:
void SetLastError( LPCTSTR szFile, LPCTSTR szFunc, int szLine, LPCTSTR szError, ... )
and a macro as:
#define SETERROR(error) SetLastError(__FILE__,__FUNCTION__,__LINE__,error)
How is am I able to edit this to accept a variable number of arguments?
|
|
|
|
|
Why is it, you spend hours looking, decide to post, then 5 minutes later you find the answer?
#define SETERROR(error,...) SetLastError(__FILE__,__FUNCTION__,__LINE__,error,__VA_ARGS__)
Works like a charm.
Now, does anybody know how to add the "__VA_ARGS__" keyword to Visual Assist?
|
|
|
|
|
waldermort wrote: Now, does anybody know how to add the "__VA_ARGS__" keyword to Visual Assist?
Who came up with the term variadic anyhow?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|