Hi I'm trying to make mfc simple project to my final assignment in oop course, but after I install the mfc
I get error of:
1. STRCPY_S
The file that have the error is read only and I can't change it. I've tried to add CRT comment and change the precomplier but no result.
error description here
the line is gives the error:
inline void __cdecl strcpy_s(
_Out_writes_z_(_S1max) char *_S1,
_In_ size_t _S1max,
_In_z_ const char *_S2)
{
ATLMFC_CRT_ERRORCHECK(::strcpy_s(_S1, _S1max, _S2));
}
Related
I a porting a large amount of code from Visual Studio to mingw unicode enabled project and I ran into this issue.It seems that the parameters I am passing from the derived class to the base class do not match. Atleast thats my assumption. I am suspecting that the issue is with the type LPCSTR Looking into this type in mingw I get the follwoing typedef
typedef CONST CHAR *LPCSTR,*PCSTR;
Now this is the base class
#ifdef UNICODE
CBasePin(
__in_opt LPCSTR pObjectName,
__in CBaseFilter *pFilter,
__in CCritSec *pLock,
__inout HRESULT *phr,
__in_opt LPCWSTR pName,
PIN_DIRECTION dir);
#endif
This is the class that inherits from the base class and passes the parameters to the base class.
CAsyncOutputPin::CAsyncOutputPin(
HRESULT * phr,
CAsyncReader *pReader,
CAsyncIo *pIo,
CCritSec * pLock)
: CBasePin(
TEXT("Text Pin"),
pReader,
pLock,
phr,
TEXT("Output"),
PINDIR_OUTPUT
),
m_pReader(pReader),
m_pIo(pIo)
,m_bQueriedForAsyncReader(false) ////
{
}
This is the linker error I am getting at the constructor initialization list
undefined reference to `CBasePin::CBasePin(wchar_t const*, CBaseFilter*, CCritSec*, long*, wchar_t const*, _PinDirection)'|
any suggestions on why I am getting this linker error. I am a bit puzzled as to why this is a linker error. I was guessing if it was a type mismatch or something it would show up as a compiler error. Any suggestions on how I can resolve this issue for Mingw 64bit gcc ?
your CBasePin has a parameter LPCSTR pObjectName (which is of type const char*) and a parameter LPCWSTR pName (whichis of type const wchar_t*) and in your call to it you're using TEXT-macro on a char-literal ,so both those literals getting the L-prefix making them const wchar_t* when UNICODE is defined ,so there is a type mismatch on the pObjectName.
If UNICODE is not defined then you would get the same error but on pName.
Remove the first TEXT and it should be Ok (for UNICODE that is).
Why are you using the TEXT macro? You have one char * parameter and one wchar_t * parameter, and it can't do the right thing for both.
Just write what you mean. Something like
"Text Pin", ..., L"Output", ...
I am receiving the following error when I attempt to compile a unit test in Visual Studio 2013:
Error 1 error C2338: Test writer must define specialization of ToString<Q* q> for your class class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > __cdecl Microsoft::VisualStudio::CppUnitTestFramework::ToString<struct HINSTANCE__>(struct HINSTANCE__ *).
You can replicate the error by having a test method such as below:
const std::wstring moduleName = L"kernel32.dll";
const HMODULE expected = GetModuleHandle(moduleName.c_str());
Microsoft::VisualStudio::CppUnitTestFramework::Assert::AreEqual(expected, expected);
Does anyone know how I need to go about writing such a specialization of ToString?
I had the same problem when comparing class objects.
For me I could resolve it by simply writing
Assert::IsTrue(bitmap1 == bitmap2);
instead of
Assert::AreEqual(bitmap1, bitmap2);
I managed to resolve the issue by adding the following code into my unit test class file:
/* ToString specialisation */
namespace Microsoft
{
namespace VisualStudio
{
namespace CppUnitTestFramework
{
template<> static std::wstring ToString<struct HINSTANCE__>
(struct HINSTANCE__ * t)
{
RETURN_WIDE_STRING(t);
}
}
}
}
I based this on the contents of CppUnitTestAssert.h (which is where the compilation error occurs - double clicking on the compilation error will open this file for you).
Near the top of the file (and only a few lines down if you double clicked on the compilation error as noted above) you can see a set of ToString templates. I copied one of these lines and pasted it into my test class file (enclosed in the same namespaces as the original templates).
I then simply modified the template to match the compilation error (specifically <struct HINSTANCE__>(struct HINSTANCE__ * t)).
For my scenario, using RETURN_WIDE_STRING(t) is sufficient in displaying a mismatch in my AreSame assertion. Depending on the type used you could go further and pull out some other meaningful text.
As of 2021, the answer Class Skeleton provided did not work for me, but I made some modifications based on it and the following compiled. Basically the lines that follows the error message provided some examples.
template<>
inline std::wstring __cdecl Microsoft::VisualStudio::CppUnitTestFramework::ToString<MyClass>(const MyClass& t)
{
// replace with your own, here is just my example
// RETURN_WIDE_STRING(t.ToString().c_str());
}
Replace MyClass with your class.
I have seen:
Convert BSTR to CHAR* and CHAR* to BSTR in C
Problems converting BSTR to char *
Convert BSTR to char*
using these question I am attempting to convert a BSTR to a char* via
#include "comutil.h"
STDMETHODIMP CServer::Initialise(BSTR strCmdFilePath,
VARIANT_BOOL bDiagErr, VARIANT_BOOL bProcErr, BSTR* RESULT)
{
char *p = _com_util::ConvertBSTRToString(strCmdFilePath);
...
}
but I am getting:
Error 1 error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)" (?ConvertBSTRToString#_com_util##YGPADPA_W#Z) referenced in function "public: virtual long __stdcall CServer::Initialise(wchar_t *,short,short,wchar_t * *)" (?Initialise#CServer##UAGJPA_WFFPAPA_W#Z)
Why am I getting this error?
Your project is not linking the required library. Which is comsuppw.lib for the Release build, comsuppwd.lib for the Debug build. Do note that you can always see the required library in the MSDN article. It is annotated at the bottom of the article. "Header" tells you what you need to #include, "Lib" tells you what you need to link.
There's an easier way for this library, the best way to get the linker instruction embedded so it is automatic is by #including the .h file that contains the #pragma comment. Fix:
#include <comdef.h> // Added
#include <comutil.h>
I am writing a C++ OpenGL project using DevIL and I am getting compile-time errors while trying to work out how to load an image to use as a texture.
So far I have this
//Declarations
const char* filename = "back.bmp";
ILboolean ilLoadImage(const char *filename);
ILuint image;
ilGenImages(1, &image);
ilBindImage(image);
//Load the image
if (!ilLoadImage(filename))
{
throw runtime_error("Unable to load image" +filename);
}
which presents me with the error: error C2110: '+' : cannot add two pointers
if I change the declaration of filename to string filename = "back.bmp"; and the if statement to
if (!ilLoadImage(const_cast<char*>(filename.c_str())))
I get this linker error error LNK1104: cannot open file 'DevIL.libkernel32.lib'
I am certain that I have placed all the DevIL files where they need to be and added the dependencies in Project->Properties->Linker->Input->Additional Dependencies.
Fix the compile error by ensuring you add C++ strings not C strings
throw runtime_error(std::string("Unable to load image") +filename);
Fix the link error by putting a space in between the libs in Additional Dependencies.
Also, if you have to use const_cast, odds are you're doing it wrong.
ILboolean ilLoadImage(const char *filename);
You don't need to cast to char * in order to pass .c_str() - .c_str() returns a const char *
I'm trying to build a console application without using the CRT, or any other imports than kernel32.lib in any case. I get my code to compile, but can't wrap the linker around a few problems:
unresolved external symbol #__security_check_cookie#4
unresolved external symbol "int __cdecl FreeLibrary(void *)" (?FreeLibrary##YAHPAX#Z)
unresolved external symbol "void * __cdecl LoadLibraryW(wchar_t *)" (?LoadLibraryW##YAPAXPA_W#Z)
unresolved external symbol "int (__cdecl*__cdecl GetProcAddress(void *,char *))(void)" (?GetProcAddress##YAP6AHXZPAXPAD#Z)
unresolved external symbol _wmainCRTStartup
FreeLibrary, LoadLibraryW and GetProcAddress I've brought in to program explicitly, not using windows.h:
#pragma comment(lib, "kernel32.lib")
typedef int(*FARPROC)();
void* LoadLibraryW( wchar_t* lpLibFileName );
FARPROC GetProcAddress( void* hModule, char* lpProcName );
int FreeLibrary( void* hLibModule );
I suppose something is wrong with my prototypes.
However, the bigger problem are __security_check_cookie and _wmainCRTStartup, which obviously have something to do with the CRT.
So I'm wondering how I'd go about overriding the default int wmain(int argc, wchar_t* argv[]) for entrypoint, and how to get rid of whatever the security cookie is.
_wmainCRTStartup is the function that calls wmain()
IIRC it should be available in some .o file that you can link with, look in your lib directory.
Maybe this is useful reading too: Reduce EXE and DLL Size with LIBCTINY.LIB (and Matt Pietrek rocks :-)
Well, answering myself here to sum up, in case someone else finds this page looking for info.
As MSalters advised, the security cookie code can be stolen from the CRT source, but doing that I found that the /GS- compiler flag can be used to avoid the security stuff altogether.
As SoapBox said, the API functions need to be __stdcall, as well as the entry point does.
I fixed the entry point issue with linker command line flag /entry:wmain.
And finally, as Tomek pointed out, the API functions gotta be in extern C!
So:
#pragma comment(lib, "kernel32.lib")
typedef int(*FARPROC)();
extern "C" {
void* __stdcall LoadLibraryW( wchar_t* lpLibFileName );
FARPROC __stdcall GetProcAddress( void* hModule, char* lpProcName );
int __stdcall FreeLibrary( void* hLibModule );
typedef int (__stdcall *f_MessageBoxW_t)( unsigned long hWnd, wchar_t* lpText, wchar_t* lpCaption, unsigned long uType);
f_MessageBoxW_t fnMsg;
void* hUser;
};
int __stdcall wmain(int argc, wchar_t* argv[])
{
hUser = LoadLibraryW( L"user32.dll" );
fnMsg = (f_MessageBoxW_t)GetProcAddress( hUser, "MessageBoxW" );
fnMsg( 0, L"foo", L"bar", 0 );
FreeLibrary( hUser );
return 0;
}
More correct entry point declaration will be:
int __stdcall wmain(PVOID ThreadParam)
Without CRT entry point called directly by BaseThreadInitThunk. Its passes pointer to something, but not argc+argv.
You can look in Windows.h to see the prototypes you need for your kernel32 imports. In general, windows functions are defined WINAPI which is actually __stdcall and not __cdecl. That will fix that problem at least.
As for your other problem, you need to explore the linker commandline arguments and see if there is a way to get it to not look for things from CRT. I don't know if there is a way to do that or not. But you're going to have to find a way or define those functions your self (which you probably don't want to do).
I'd recommend just using a different compiler/linker.
You need to declare windows.h functions as extern "C".
The proper entry point is main(), not wmain() (since you're compiling a console app).
The security cookie code can be nicked from the CRT source code; no need to link it in.