flavour404
flavour404

Reputation: 6312

CString error, 'CString': is not a member of 'ATL::CStringT<BaseType, StringTraits>'

I am trying to do this:

#include <atlstr.h>

CHAR Filename; // [sp+26Ch] [bp-110h]@1
char v31; // [sp+36Ch] [bp-10h]@1
int v32; // [sp+378h] [bp-4h]@1

 GetModuleFileNameA(0, &Filename, 0x100u);
 CString::CString(&v31, &Filename);

But I am getting the compiler error C2039:'CString': is not a member of 'ATL::CStringT'

This is a non MFC based dll, but according to the docs you should be able to use CString functionality with the include #include atlstr.h how do I make it work?

Thanks

Upvotes: 1

Views: 4954

Answers (2)

TheUndeadFish
TheUndeadFish

Reputation: 8171

You have several problems in this code snippet:

1) CHAR Filename; declares a variable that is only a single character. However, GetModuleFileNameA expects to be given a pointer to an array of characters. When you pass the parameters &Filename and 0x100u you would make it think that &Filename points to an array of memory with room for up to 256 characters. However, as written in your snipped, it's only a single character. Thus you would have a bad buffer overflow.

Filename should most likely be declared as CHAR Filename[0x100]; in this case. That would also mean you don't need to take the address of Filename when passing it to that function. So the call would then be written as GetModuleFileNameA(0, Filename, 0x100u);

2) When writing code for a constructor, you define is by writing something similar to CString::CString (using whatever your class's name is) and then filling out the function. However, when using a constructor you don't use that syntax at all. You don't call CString::CString() to create a CString object.

You would have to choose an name for the CString object, such as "FilenameStr". So the in the context of you code you would write something like CString FilenameStr(Filename);

3) As implied at the end of the last point, the parameters you are trying to pass to the constructor are wrong. &v31 and &Filename would each by pointers to characters in your original code. However, as far as I know, CString does not have any constructor that takes two character pointers.

I can't even tell how v31 is supposed to be involved there, but it doesn't seem necessary at all. If you want to fill a CString with the contents of a character array, then you can just pass that array to the constructor and it will take care of everything. So, something like CString FilenameStr(Filename);

Upvotes: 0

Alexander Gessler
Alexander Gessler

Reputation: 46607

That's not how constructors are invoked in C++.

CString s = CString(&v21,&File);

Note that GetModuleFilename expects a pointer to an array of characters (which it fills), not a pointer to a single character. Your code is therefore doomed to crash at runtime.

Upvotes: 1

Related Questions