My code:
//the auto generated stuff (by right-click on editbox add variable to control option)
CEdit edit_name;
void CSendMessageWithActualDataDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_NAME, edit_name);
}
Looks OK so far, right?
void dofoo()
{
//IDE shows error squiggle at dot "name.GetWi" with no pop-up
//details given except the object's blurb text
CString foostring= edit_name.GetWindowTextW();
//error C2661: 'CWnd::GetWindowTextW': no overloaded function takes 0 arguments
// or instead with no parentheses added:
// no IDE error indication
CString foostring= edit_name.GetWindowTextW;
//error C3867: 'CWnd::GetWindowTextW': non-standard syntax; use '&' to create a pointer to member
}
According to:
Read text from edit control in MFC and VS2010
You can then retrieve the text in the edit box like this:
CEdit m_EditCtrl;
// ....
CString filePath1 = m_EditCtrl.GetWindowText()
.
.
.
// Yes, now it works...
I swear, this is like pulling teeth.
Apparently, this is all about convenience. Since you already know how to call CWnd::GetWindowText, but find it too cumbersome to use, just wrap everything up in a free function:
CString GetWindowText(CWnd const& wnd) {
CString s;
wnd.GetWindowText(s);
return s;
}
You can call that, passing in any object that (publicly) derives from CWnd (such as the CEdit edit_name) and get a CString object you can use any which way. (C++17 introduced guaranteed copy elision, meaning that s will never need to get copied.)
If you need to pass it into other Windows API functions that expect an LPCTSTR, CString implements operator PCXSTR that implicitly converts things as needed, e.g.
AfxMessageBox(GetWindowText(edit_name));
When you add a EDIT control to a dialog, and you go to map it to a variable, you have two choices:
Control
Value
If you select Value, then you can map it to a CString:
This creates the DDX_Text entry that was referred to in the comments:
void CTestDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_strText);
}
Or, you can use GetDlgItemText if you want to just get the value directly from the control, and avoid creating a variable. This shows both:
void CTestDialogDlg::OnBnClickedButton1()
{
// This way you get the current text value from the edit control, without even creating a CEdit control member variable
CString strText;
GetDlgItemText(IDC_EDIT1, strText);
AfxMessageBox(strText);
// This way we use our mapped CString to the EDIT control
UpdateData(TRUE);
AfxMessageBox(m_strText);
}
If you do decide to map to a CEdit (a Control instead of a Value) then as you found out:
// Get the value from the control
m_editText.GetWindowText(strText);
AfxMessageBox(strText);
I assume your linked article is incorrect as GetWindowText does not return a CString.
By the way, with Visual Assist (a third-party extension) it shows the options correctly for Intellisense:
By the way, you may find this article useful (Dialog box controls and variable types). If you look at the table and find EDIT:
The above is with the previous rows on the table stripped out. As you can see, you can map the EDIT to more types of variables than just a CString.
this suffices:
class GetWindowText
{
CString s;
public:
GetWindowText(CWnd &w){w.GetWindowText(s);}
~GetWindowText() {}
CString operator()() {return s;}
};
just call like this:
MessageBoxW(GetWindowText(edit_name)());
Related
I am working with CDialogs and mfc. My dialog loads, and there are two input boxes and a button. When i click the button I want to have the values in the input boxes change. My current attempt is this
void
CInstanceNumberDlg::updateLeftRange(int i) {
GetDlgItem(IDC_2NDDERIV_WT)->SetWindowText((LPCTSTR)i);
UpdateData(false);
UpdateWindow();
}
I have also tried with other combinations of including UpdateData() and UpdateWindow(). This method is being reached with a valid integer, and that ID should be valid.
How can i get the value in the input box to be modified?
Thank you
Your problem is this line:
GetDlgItem(IDC_2NDDERIV_WT)->SetWindowText((LPCTSTR)i);
i is not a pointer to a string and so you should not be casting it to an LPCTSTR. What you need to do convert the value of i to a string and then pass a pointer to the resulting string to SetWindowText(). And I don't believe you need UpdateData() or UpdateWindow(). For example:
void CInstanceNumberDlg::updateLeftRange(int i)
{
TCHAR tszValue[32];
StringCchPrintf(tszValue, _countof(tszValue), TEXT("%d"), i);
GetDlgItem(IDC_2NDDERIV_WT)->SetWindowText(tszValue);
}
I recently started to work on application interception using madhookc hooking library.I am hooking the methods called in the application to do manipulation of text. At one point i got stuck and need your help.
I have a paragraph of text in which in which some text is Bold and some is regular style. Now i want to differentiate the regular and bold text and do different operations on both. I know that QFont::setFamily(QString) function is called for each line of text that is going to be displayed.So i hooked setFamily(). Now i am able to get font family of the text ,but all text of the paragraph (i.e. regular and bold) has same font family. Now i want to check whether this text is bold or not, so for that i need a object of QFont class so that i can call it's bold(); which returns true or false. Ultimately i want this pointer of the setFamily() function.(Same as like we get target in pointcut of AspectJ interception.)
void WINAPI newsetFamily( QString & family );
void (WINAPI *UnhooksetFamily)( QString & family );
void WINAPI newsetFamily ( QString & family )
{
QFont *font=this_pointer;
if(font->bold())
{
//do this
}
else
{
//do this
}
}
Please help me . Thanx in advance. . .
Finally i got my way !!!
After some research , i come to know that that current class object of a function is nothing but this pointer of the function, and this pointer can be exist only if the function belongs to some class.
I also came to know that QT class functions follows various calling conventions.The class i am referring follows the __fastcall for its functions. __fastcall calling convention requires first 2 hidden parameters to be passed to function if you want to hook it successfully. So i passed void * (void pointers) as first 2 parameters of the function and it worked.
Then i came to know that first void * pointer is nothing but this pointer of the function. That's how i got my target.
solved code:
void (__fastcall *UnhooksetFamily)( QFont *This,void *noUse,QString & family );
void __fastcall newsetFamily ( QFont *This,void *noUse,QString & family )
{
QFont *font=This;
if(font->bold())
{
//do this
}
else
{
//do this
}
}
As previously discussed here, I'm trying to find a workaround for the LNK2019 issue that arises when building a static library which utilizes C++ templates, and separating the source from the header to keep the code private from other projects. I believe I've nearly come to a working conclusion (for my particular situation), but I'm not entirely sure if this is the correct/best way to go about it and was wondering if anyone has any suggestions, improvements/comments to add?
The goal is to do some type checking to see if the template's signature matches the target prototype function's signature, do some private processing, and return whether or not it was sucessful. NOTE that I have removed SdkHookMgr.h and SdkHookMgr.cpp from the prior version of the solution in the above link, and merged everything back into SdkLib.h and SdkLib.cpp, into a static class for a bit of clarity.
SdkLib.h:
#include <typeinfo>
#ifdef MY_EXPORTS
# define MYDECL __declspec(dllexport)
#else
# define MYDECL
#endif
// Prototypes
typedef HMODULE (WINAPI *HookLoadLibraryA)( LPCSTR lpFileName );
//...
class CHook;
class CHookManager;
MYDECL BOOL WINAPI ValidateHook( CHook *hook );
class CHook
{
public:
CHook() : m_type(NULL), m_target(NULL), m_result(FALSE) {};
CHook( const char *type, PVOID target ) : m_type(type), m_target(target) {
m_result = ValidateHook(this);
};
const char *m_type;
PVOID m_target;
BOOL m_result;
};
class CHookManager
{
public:
template <typename HookFunction> static BOOL Hook(HookFunction target)
{
const type_info& type = typeid(HookFunction);
CHook *hook = new CHook( type.name(), target );
return hook->m_result;
}
};
SdkLib.cpp:
#include <SdkLib.h>
IDXDECL BOOL WINAPI ValidateHook( CHook *hook )
{
// Do type checking, private processing, etc here...
return TRUE;
}
DemoDLL.cpp:
#include <SdkLib.h>
HMODULE WINAPI Hooked_LoadLibraryA( LPCSTR lpFileName )
{
DebugBreak();
}
// The function that starts the rollercoaster.
// - Syntax: Hook< prototype >( target )
if!(CHookManager::Hook<HookLoadLibraryA>(Hooked_LoadLibraryA))
cout << "Failed to create hook for LoadLibraryA!" << endl;
You may find that the results of typeid are not consistent between the DLL and the main program. (See, for example, typeid result across different dll's.)
Since your list of possible hooks is limited, it strikes me that overloaded functions would be a better choice than templates. You'd then have no DLL issues, and the validity of each hook would be checked at compile time. Here's an example of the sort of thing I'm thinking of; obviously in practice you'd split this into separate definition and declaration, with the definitions living in the DLL so it's all cleanly separated out.
class CHookManager {
public:
BOOL Hook(HookLoadLibraryA hook) {
assert(sizeof hook<=sizeof(uintptr_t));
return ValidateHook((uintptr_t)hook,"LoadLibraryA");
}
BOOL Hook(HookLoadLibraryW hook) {
assert(sizeof hook<=sizeof(uintptr_t));
return ValidateHook((uintptr_t)hook,"LoadLibraryW");
}
};
(Note that this shows up one disadvantage of this approach - you can only have one hook per function signature. I mention this for completeness' sake, but I'll assume this hasn't proven an issue.)
(You might like to replace the assert with a compile-time assert, if you have one.)
ValidateHook would use strcmp to figure out which hook is being hooked. Once it's figured out which hook it is, it would then cast the uintptr_t to the appropriate function pointer type. It knows the pointer was originally of the correct type for that hook, because you're using the C++ overload mechanism to do it all. (Or you could have an enum, say, for all the hook types, rather than passing in a string - it's up to you. The key part is that you have full control over the values being passed, so that the DLL and the calling code are definitely using matching values.)
This code would be a little tiresome to generate, but if you already have the list of typedef names then you could create the corresponding code using regular expression search and replace, or keyboard macros, in your editor of choice. Or you could use something like the so-called "X-Macro" to automate the generation of the whole thing.
A little background:
I've got a library of C code that is part of larger system (all C). For this particular part of the C library a GUI has to be created which allows users to play around with the options. For the GUI I've chosen QT, since cross-platform support is desired.
I'm using Eclipse and MinGW as IDE and compiler (but I think the question is more language than compiler specific?).
With QT I've created a widget that holds a pointer to a struct implemented in C that contains pointers to several functions that perform the logic of the C library.
//QTWidget.cpp
extern "C" {
#include "c-src/CLogic.h"
//extern char* textHelper;
}
QTWidget::QTWidget(QWidget *parent)
{
//rtw is a struct that contains a function pointer to a member of QTWidget
this->rtw.displayText = &QTWidget::displayText;
this->clogic = CLogic_getInstance(&rtw);
}
//Public SLOT, connected to a button's clicked SIGNAL
void QTWidget::buttonClicked()
{
this->clogic->buttonClicked();
}
void QTWidget::displayText(char *text, int position)
{
//I've tried creating a QString from the char*, but this does not work at all.
//ui.textItem->setText(textHelper);
ui.textItem->setText(text);
}
When the user presses a button in the GUI, the method QTWidget::buttonClicked() is called, which tells the C library to do something. Note the the CLogic struct has a reference to the the QTWidget in the form of a struct RefToWidget which holds a function pointer.
//CLogic.c
static CLogic instance;
void CLogic_buttonClicked()
{
//I've tried several variants here, such as making a global
//char* textHelper = "Hello World";
//that is referenced by using the "extern" keyword in the CPP file above.
instance.rtw->displayText("Hello World", 1);
}
CLogic* CLogic_getInstance(RefToWidget *rtw)
{
instance.rtw = rtw;
instance.buttonClicked = &CLogic_buttonClicked();
}
When debugging this program, I find that all the function calls are executed as intended (when I press a button, the QT slot buttonClicked() is called, the CLogic_buttonClicked() is called, which calls the QTWidget::displayText() as planned, but in this last call the parameters are invalid. The char* text points to 0x1 and claims to be pointing to memory out of bounds, while the int position looks like some random number (uninitialized).
How do I pass this data from C to CPP?
EDIT #Luccas Matteis:
#ifdef __cplusplus
#include "QTWidget.h"
extern "C" {
#endif
struct RefToWidget{
#ifdef __cplusplus
void (QTWidget::*displayLine)(char* text, int lineNumber);
#else
void (*displayLine)(char* text, int lineNumber);
#endif
};
typedef struct RefToWidget RefToWidget;
#ifdef __cplusplus
}
#endif
As said above the function calls behave as expected, but the data is not passed "correctly" (even though that when I look at it, the code seems a bit... weird... ;))
Your problem is not passing the char * from C to C++ but calling a C++ function from C. I presume that CLogic.c is compiled as a C library? If not, can you rename it to CLogic.cpp or use a compiler switch to force it to be compiled as C++ even though it has a C file extension?
If you want a C++ GUI on a C library/system, you need to use proper Model-View-Controller logic. Here the C++ code is the View and the Controller while the C code is the Model (as best as I can tell from your description). You need to make it so you set and get data from the model but the model never calls the View or Controller as you are trying to do.
Think about what you are REALLY trying to do. If you just want to display a static string when the button is pressed, why go to the bother of calling into CLogic.c? If you want to display a string that depends upon the state of the CLogic instance then instead do something like:
void QTWidget::buttonClicked()
{
char *display_text = this->clogic->get_button_click_text();
ui.textItem->setText(display_text);
}
I guess the problem is that the structure holds a pointer to a member function. The member function probably expects the first parameter to be 'this' - the object it refers to. So, in fact what you are seeing in debug as text is the second parameter.
A 'solution' would probably be to do something like "instance.rtw->displayText(instance.rtw, "Hello World", 1)", but have no idea if it is portable etc.
Edit: Saying it explicitly: the 'solution' stated above is just to try and check if this is the problem. As the comments are saying this is a horrible hack that might not work even on the same compiler.
I don't think that you should call a C++ method from C. Normally you need to go through a static method that dereferences a pointer argument
I've already found a workaround to this problem, but was just wondering if anyone knew what was actually happening to cause the problem I was seeing. My guess is that it has something to do with mutability of strings, but I thought the CString object accounted for that in the copy constructor.
The following code causes mFileName to be overwritten:
class File {
public:
...
CString GetFilename() {return mFileName;}
private:
CString mFileName;
};
class FileContainer {
private: File* mFile;
public:
FileContainer() {
mFile = new File("C:\temp.txt");
}
GetFilename(CString& fileName) {
fileName = mFile->GetFileName();
}
}
void UpdateText() {
FileContainer fileCnt;
CString filePath(L"");
this->fileCnt.GetFilename(filePath);
...
::DrawText(hDC, filePath, -1, &destRect, DT_PATH_ELLIPSIS | DT_MODIFYSTRING | DT_CALCRECT);
}
What happens is that the first time UpdateText is called, GetFilename returns C:\temp.txt. Assuming that the bounding rect caused the text to be truncated to "...\temp.txt" on the first call, "...\temp.txt" is what is returned from GetFilename on the second call to UpdateText.
Even more perplexing is that this didn't cause mFileName to be changed:
void UpdateText() {
FileContainer fileCnt;
CString filePath(L"");
this->fileCnt->GetFilename(filePath);
filePath = L"TEST";
}
GetFilename always returned C:\temp.txt. So it would seem that the DrawText function is somehow finding the original CString and modifying it. But how?
UPDATE: I figured I'd throw another odd chunk of code that also causes mFileName to be overwritten:
class File {
public:
...
CString GetFilename() {return CString(mFileName);}
private:
CString mFileName;
};
That seems like it should create a new object and return that new object. Yet, somehow, DrawText still overwrites mFileName.
If I change the code to the following, I don't have any issues:
class File {
public:
...
CString GetFilename() {return CString(mFileName.GetBuffer());}
private:
CString mFileName;
};
The only thing that seems to solve the problem is to construct a new CString the way I showed in the workaround. What is DrawText doing when I pass the DT_MODIFYSTRING option?
First, note that CString can be used as a raw string pointer in two ways:
operator LPCSTR - gives a pointer which should never be modified.
GetBuffer - gives a pointer to memory specifically for the purpose of modifying the string.
Now, DrawText is declared to accept a LPCSTR. So when you pass a CString object directly as in your code, it implicitly uses operator LPCSTR to give the function what it says it wants, a constant string pointer.
However, DT_MODIFYSTRING says that DrawText can modify the string it was given. So internally, DrawText must be throwing away the constness of the pointer and modifying the string anyway.
This combination is a bad thing. But the fault is mainly in the implmentation of DrawText which is violating its own declaration.
As for why this modifies other CString objects: Apparently when a CString object is copied, it delays copying the internal string memory until something tries to modify the string through a CString member function. But until that happens, the operator LPCSTR of each CString object would still point to the same shared internal memory. This is normally fine, as long as any code using it is obeying the rules of const-correctness. However, as we've already seen, DrawText with DT_MODIFYSTRING is not playing by the rules. Thus, it is overwriting memory shared by multiple CString objects.
So to fix this problem, you either need to stop using DT_MODIFYSTRING if you don't actually need the modified text. Or else you need to pass the string to DrawText using filePath.GetBuffer() and then call filePath.ReleaseBuffer() afterwards.
Well there are some discrepancies in the code that you posted:
In 'class File':
GetFileName() {return mFileName;}
There is no return type here? Also in the FileContainer class you define the stored 'File' object as a pointer, but in the GetFileName function you access it as if it was not a pointer?
File* mFile;
...
mFile.GetFileName();
As far as why this is happening well right now I can't really tell. Another work around however would be to change the GetFileName function to return a const ref, this should ensure that the returned value can never be changed.
const CString& GetFileName() { return mFileName; }