I have a funny situation. I am sitting on a computer on which there is a shared folder, say, "My_Folder". So, I can access it using the address "\My_Box\My_Folder".
Now, if I write a small tool in C#, I can convert the "\My_Box\My_Folder" to "C:\My_Folder" which is its physically location.
The funny thing is that I don't know how to find the physically location of "My_Box\My_Folder" without my program ... And it's actually one of my friend's problem.
So if anyone knows how to find the local path from a network path with some simple basic Windows commands/operations (either Winxp, 2000, vista, 7, ...), please let me know.
Thanks,
Pete.
If all you need is a command line tool that will provide this information then you can simply use the net share command that’s built into Windows.
If you need to do it programmatically you can use the NetShareGetInfo function. The following example shows how to use it to query the path for \\localhost\share in C++.
#include <windows.h>
#include <lm.h>
#pragma comment (lib, "netapi32.lib")
#include <iostream>
using namespace std;
int main()
{
BYTE * buffer = nullptr;
auto error = NetShareGetInfo(nullptr, // local computer
L"share",
2, // level
&buffer);
if (ERROR_SUCCESS == error)
{
auto info = reinterpret_cast<SHARE_INFO_2 *>(buffer);
wcout << info->shi2_path << endl;
}
if (buffer)
{
NetApiBufferFree(buffer);
}
}
I would wrap the buffer in a class and call NetApiBufferFree from its destructor but that’s up to you.
Related
I use VSCode to create the .exe file of the simple following C++ code:
#include <iostream>
using namespace std;
int main()
{
int x = 25;
cout << x << endl;
return 0;
}
When I open the .exe program created, the window appears and shuts down immediately. If I put getchar() before return 0, I can make the program wait for user input to fix it. However, if I use Dev-C or Codeblocks, this problem does not happen. In other word, I do not need to add getchar() to hold the window. Hope that someone can explain what problem occurs and how can I fix it to make the .exe created by VSCode run normally? Thank you so much.
When I try to declare a c++ vector, and initialize it at the same time, I get an error. This only happens to me in codelite - the same exact code works in xcode.
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector <int> myvec {32, 234, 25, 235};
cout<<myvec[1];
return 0;
}
The error is "expected ';' at end of declarion." Then, it shows an arrow pointing in between "myvec" and "{32,234....};", and says to put a ';' there. I know this sounds a little unclear - I can provide a screenshot if necessary.
Thanks for the help.
I'm aware of many similar questions on this site. I really like the solution mention in the following link:
https://stackoverflow.com/a/25021520/884553
with some modification, you can include text file at compile time, for example:
constexpr const char* s =
#include "file.txt"
BUT to make this work you have to add string literal prefix and suffix to your original file, for example
R"(
This is the original content,
and I don't want this file to be modified. but i
don't know how to do it.
)";
My question is: is there a way to make this work but not modifying file.txt?
(I know I can use command line tools to make a copy, prepend and append to the copy, remove the copy after compile. I'm looking for a more elegant solution than this. hopefully no need of other tools)
Here's what I've tried (but not working):
#include <iostream>
int main() {
constexpr const char* s =
#include "bra.txt" // R"(
#include "file.txt" //original file without R"( and )";
#include "ket.txt" // )";
std::cout << s << "\n";
return 0;
}
/opt/gcc8/bin/g++ -std=c++1z a.cpp
In file included from a.cpp:5:
bra.txt:1:1: error: unterminated raw string
R"(
^
a.cpp: In function ‘int main()’:
a.cpp:4:27: error: expected primary-expression at end of input
constexpr const char* s =
^
a.cpp:4:27: error: expected ‘}’ at end of input
a.cpp:3:12: note: to match this ‘{’
int main() {
^
No, this cannot be done.
There is a c++2a proposal to allow inclusion of such resources at compile time called std::embed.
The motivation part of ths p1040r1 proposal:
Motivation
Every C and C++ programmer -- at some point -- attempts to #include large chunks of non-C++ data into their code. Of course, #include expects the format of the data to be source code, and thusly the program fails with spectacular lexer errors. Thusly, many different tools and practices were adapted to handle this, as far back as 1995 with the xxd tool. Many industries need such functionality, including (but hardly limited to):
Financial Development
representing coefficients and numeric constants for performance-critical algorithms;
Game Development
assets that do not change at runtime, such as icons, fixed textures and other data
Shader and scripting code;
Embedded Development
storing large chunks of binary, such as firmware, in a well-compressed format
placing data in memory on chips and systems that do not have an operating system or file system;
Application Development
compressed binary blobs representing data
non-C++ script code that is not changed at runtime; and
Server Development
configuration parameters which are known at build-time and are baked in to set limits and give compile-time information to tweak performance under certain loads
SSL/TLS Certificates hard-coded into your executable (requiring a rebuild and potential authorization before deploying new certificates).
In the pursuit of this goal, these tools have proven to have inadequacies and contribute poorly to the C++ development cycle as it continues to scale up for larger and better low-end devices and high-performance machines, bogging developers down with menial build tasks and trying to cover-up disappointing differences between platforms.
MongoDB has been kind enough to share some of their code below. Other companies have had their example code anonymized or simply not included directly out of shame for the things they need to do to support their workflows. The author thanks MongoDB for their courage and their support for std::embed.
The request for some form of #include_string or similar dates back quite a long time, with one of the oldest stack overflow questions asked-and-answered about it dating back nearly 10 years. Predating even that is a plethora of mailing list posts and forum posts asking how to get script code and other things that are not likely to change into the binary.
This paper proposes <embed> to make this process much more efficient, portable, and streamlined. Here’s an example of the ideal:
#include <embed>
int main (int, char*[]) {
constexpr std::span<const std::byte> fxaa_binary = std::embed( "fxaa.spirv" );
// assert this is a SPIRV file, compile-time
static_assert( fxaa_binary[0] == 0x03 && fxaa_binary[1] == 0x02
&& fxaa_binary[2] == 0x23 && fxaa_binary[3] == 0x07
, "given wrong SPIRV data, check rebuild or check the binaries!" )
auto context = make_vulkan_context();
// data kept around and made available for binary
// to use at runtime
auto fxaa_shader = make_shader( context, fxaa_binary );
for (;;) {
// ...
// and we’re off!
// ...
}
return 0;
}
I'm just starting learning C++/XAML windows store app development but for the life of me I can't find a nice way to print variable values to the "Output" window in VS2012.
Debug.WriteLine() doesn't seem to exist for Windows Store apps and I can't find a way to print other than OutputDebugString() which I can't use to print variable values (without some heavy formatting).
Is there just an easy way to print the example line:
mouse position X: 12
for example, where 12 is an integer that comes from MouseDelta.
Thanks for your time,
Poncho
Not really, no. You could write a little function that formatted like printf and passed along the resulting string to OutputDebugString() but there's nothing more straightforward available.
I guess you could use ToString(), Platform::String::operator+, and Platform::String::Data() to accomplish this; though it's a little ugly:
OutputDebugString( ("mouse position X:" + MouseDelta.X.ToString())->Data() );
Here is a nice alternative: http://seaplusplus.com/2012/06/25/printf-debugging-in-metro-style-apps/, basically it allocates a console for your Windows Store App, obviously this will fail certification but given that this may be just for debug purposes only, it will fine. I'm copying the relevant code here:
// Include Windows.h for WINBASEAPI and WINAPI:
#include <Windows.h>
// Declare AllocConsole ourselves, since Windows.h omits it:
extern "C" WINBASEAPI int WINAPI AllocConsole();
auto main(Platform::Array<Platform::String^>^) -> int
{
AllocConsole();
std::wcout << L"Hello there!" << std::endl;
std::getchar();
return EXIT_SUCCESS;
}
However if you want to see such output inside your app, then you may want to use Console Class for Modern UI Apps which implements part of the .NET System.Console and can be safely used inside Windows Store apps.
This solution uses a wrapper around OutputDebugString:
void WinLog(const wchar_t *text, int n)
{
wchar_t buf[1024];
_snwprintf_s(buf, 1024, _TRUNCATE, L"%s %d\n", text, n);
OutputDebugString(buf);
}
which can be called as follows:
WinLog(L"The Answer is", 42);
I am getting the following error in my program:
error D8016: '/ZI' and '/clr' command-line options are incompatible
This happens when I put the following lines and enable common runtime in configuration->General (If I dont enable it then the error will come at using system and System::Drawing )
#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
Actually I will be using some windows library in my code that requires the above dll.
How to solve this issue?
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc/imgproc_c.h>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace std;
int main( int argc, char** argv )
{
IplImage *source = cvLoadImage( "Image.bmp");
// Here we retrieve a percentage value to a integer
int percent =20;
// declare a destination IplImage object with correct size, depth and channels
IplImage *destination = cvCreateImage
( cvSize((int)((source->width*percent)/100) , (int)((source->height*percent)/100) ),
source->depth, source->nChannels );
//use cvResize to resize source to a destination image
cvResize(source, destination);
// save image with a name supplied with a second argument
cvShowImage("new:",destination);
cvWaitKey(0);
return 0;
}
In visual studio to turn off /ZI:
Open the project's Property Pages dialog box.
Click the C/C++ folder.
Click the General property page.
Modify the Debug Information Format property - set it to "None"
Upgrading VS will help. Minimum version: 16.11.11
In addition to what the Answer by PGP suggests, consider also changing C/C++ -> Optimization -> Optimization to Disabled (/Od).
Having it as Maximum Optimization (Favor Speed) (/O2) might give you problems when compiling for debug.
-O2 it's a certain level of compile-time optimisation. Google about what it does
In VS2017:
\ZI is set by C/C++>General>Debug Information Format = Program Database for Edit and Continue
\GL is set by C/C++>Optimization>Whole Program Optimization = Yes
I copied a configuration that I wanted to use as debug config and ran into that issue.