printf doesn't print any message from dll library - c++

I have problem in printing messages from ".dll" library created in visual studio 2019.
I don't receive any errors or warnings. printf or std::cout just do nothing. Everything worked fine in visual studio 2010. I tried to compare project properties between vs2019 and vs2010 versions. But I didn't notice anything that may effect output.
Here is simple example that shows how I expect to receive messages from this library:
#define DLLReturnType __declspec(dllexport)
DLLReturnType int add3DArraysLib(int value)
{
printf("Received value - %d", value);
return value + 1;
}

My suggestion is to change your "SubSystem" to be Console (/SUBSYSTEM:CONSOLE) then to see the printf messages on Visual Studio Output window.
One more thing, #include <iostream> is necessary !
Property setting is here:

Related

"unknown error" from std::error_code on Windows

I have a strange problem with system error messages obtained from std::error_code on Windows. When I build and run test program 1 (see below) using my locally installed Visual Studio, error messages for all system error codes come out as "unknown error". On the other hand, when building and running the same program on the same version of Visual Studio through Godbolt / Compiler Explorer, proper error messages are produced (see test program 1 and output below).
I tested this with Visual Studio 2022 version 17.3.3 (MSVC 19.33) on Windows 10.
I am using the community version of Visual Studio.
I have tried to build locally using the developer command prompt (cl test.cpp), using a Visual Studio console project (all settings at default), and using a Visual Studio CMake project (all settings at default). It makes no difference. In all cases, all error messages come out as "unknown error".
I am not experienced with Visual Studio, so I may definitely be making a very basic mistake.
Any advice as to how I can further diagnose the problem is also welcome.
Output from test program 1 (see below) when built and run through Godbolt / Compiler Explorer:
message = 'The directory is not empty.' (193331629)
Output from test program 1 (see below) when built and run locally:
message = 'unknown error' (193331629)
Output from test program 2 (see below) when built and run locally:
message = 'The directory is not empty.' (193331629)
Test program 1:
#include <windows.h>
#include <system_error>
#include <stdio.h>
int main()
{
std::error_code ec(ERROR_DIR_NOT_EMPTY, std::system_category());
printf("message = '%s' (%lld)\n", ec.message().c_str(), static_cast<long long>(_MSC_FULL_VER));
}
Test program 2 (for contrast):
#include <windows.h>
#include <stdio.h>
int main()
{
char buffer[256];
DWORD len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, ERROR_DIR_NOT_EMPTY, 0, buffer, 255, NULL);
while (len > 0 && (buffer[len - 1] == '\n' || buffer[len - 1] == '\r'))
--len;
buffer[len] = '\0';
printf("message = '%s' (%lld)\n", buffer, static_cast<long long>(_MSC_FULL_VER));
}
Ok, the problem in my case is that the system locale is set to "en-GB" and not "en-US".
If I pass language identifier 2057 (en-GB) to FormatMessage() I get no error message, but if I pass 1033 (en-US), I do.
So far, I have not been able to change the system-level locale, but even if it can be done, it seems rather suboptimal that system error messages fail to work if my system locale is set to "en-GB".
I wonder if there is a rational idea behind this behavior, or if it is just plain broken.
In any case, the solution in my case, I think, is to introduce a custom error category that invokes FormatMessage() with a language identifier of 0 (see https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage), and then forward to the native system error categeory in the functions that deal with mapping to generic error condition.

Error code = 0x80070002 in visual studio c++

I just installed visual studio, (not new to coding) and I keep getting an error that goes like "error: unable to open file C:user...main.obj Error code = 0x80070002"
an Image of the error
this error shows up whenever there is something wrong with my code like for example if i do:
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
}
everything runs normal, but if I make a mistake say:
#include <iostream>
int main()
{
std::cout std::cout << "Hello World!\n";
}
I get the error, it's so annoying I don't want to get that error every time there is something wrong with my code the error list bar is enough, what do I do ?
Could you please tell us what version of Visual Studio are you using?
If you are using vs2017 or later, I suggest you could try to use /DEBUG:FULL property in "Linker/Debugging/Generate Debug Info.
I suggest you could refer to the Doc:
When you specify /DEBUG with no additional options, the linker
defaults to /DEBUG:FULL for command line and make file builds, for
release builds in the Visual Studio IDE, and for both debug and
release builds in Visual Studio 2015 and earlier versions. Beginning
in Visual Studio 2017, the build system in the IDE defaults to
/DEBUG:FASTLINK when you specify the /DEBUG option for debug builds.
Other defaults are unchanged to maintain backward compatibility.
Rebuild the program in a different disk or reinstall the VS could be the final solution I think.

Visual Studio 2015 + libtcod - Unexpected exit

I am having an odd issue getting libtcod to work with Visual Studio 2015.
I have followed all the steps found on this blog post to get everything linked.
The problem is that during debugging or running it inside Visual Studio the application will always close at initRoot with exit code 1, no other information or errors.
Running the produced EXE outside of Visual Studio has no issues whatsoever - starts up and works as expected!
The only information I could find related to this was a forum post that turned ugly in 2012
I did make a modification to the blog post's code to include a setCustomFont as well, which did not change anything.
Does anyone have any ideas on what might be going on?
Code:
#include <cstdio>
#include "libtcod.hpp"
// Hide the console window
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
int main()
{
TCODConsole::setCustomFont("terminal12x12_gs_ro.png", TCOD_FONT_LAYOUT_ASCII_INROW | TCOD_FONT_TYPE_GRAYSCALE);
TCODConsole::initRoot(80, 50, "C++ libtcod tutorial");
TCODConsole::root->printEx(40, 25, TCOD_BKGND_NONE, TCOD_CENTER, "Hello world");
TCODConsole::flush();
TCODConsole::waitForKeypress(true);
return 0;
}
For anyone that stumbles upon this later, the above blog post is correct for setting up the vast majority of the application - if you run into the same issue I did (crashing / exiting in initRoot) make sure you have the terminal png in the source directory, this is what fixed it for me.

Outputdebugstring, printf not work(Visual studio 2012, Windows 8)

I'm using Visual studio 2012 on Windows 8.
I usually use OutputDebugString for logging, but recently it doesn't work.
I reinstall visual studio too, but it was same.
Now I can do logging with only fprintf!! It's really painful....
Anyone has solution about this problem?
-- my code --
#include <stdio.h>
#include <Windows.h>
int main()
{
OutputDebugString( L"hello world" );
printf("hello world\n");
return 0;
}
I made new project right now,
This project's printf is work well. But OutputDebugString still doesn't work.
And my cocos2dx project( yeah, i'm working on cocos2dx ), it's both printf and OutputDebugString doesn't work!!
Make sure your system debug print filter is set to allow all messages, typically done by setting the registry value HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Debug Print Filter to DWORD 0xFFFFFFFF.
See http://msdn.microsoft.com/en-us/library/windows/hardware/ff551519(v=vs.85).aspx for more.

wchar and stdout in VC11

I've found a weird issue with outputting wide chars in Visual Studio 2012 which I've narrowed down to the following code.
#include <cstdio>
int main()
{
fputws(L"Hello World\n", stdout); // Throws Access Violation exception
}
When compiling this with Visual C++ 2012 it throws an "Unhandled exception", "Access violation reading location 0x00000064", somewhere inside fputws.
What I'm using to compile is with is the CLI version, just to rule out any settings in the IDE. I am opening the Visual Studio Command Prompt and using the following:
cl test.cpp
When using Visual Studio 2008 or Visual Studio 2010 it works well, writing out "Hello World".
But when using Visual Studio 2012, it crashes with the above mentioned error.
I have a hard time believing it's a compiler issue but rather something that's changed between the different versions of C++.
Another (funny) thing is that if I output a normal char first, like the code snippet below, it works just fine. So what I think is that it is an issue with uninitialized streams?
#include <cstdio>
int main()
{
fputs("", stdout);
fputws(L"Hello World\n", stdout); // Now this works.
}
Anyone got any ideas?
Edit:
The following, similar, functions works fine in VS2012:
std::wcout << L"Hello world" << std::endl;
wprintf(L"Hello world\n");
_putws(L"Hello\n", stdout);
putwchar(L'H');
Edit 2:
Just filed a bug report to microsoft.
I had this problem on a RTM VS2012 but didn't on VS2012 Update 1. After installing Update 1 on the problem system the error disappeared. Thanks to Mats for reporting this.