How to get running application name in UWP? - c++

I have a C++ dll that is used by different UWP apps. Inside my dll I want to get the name of the running app. Standard winapi functions seem to not work, because they return exe names of some "wrappers" (e.g. ApplicationFrameHost.exe...).

ITNOA
As you can see [UWP]How to get running application name in UWP?, Azat Tazayan says you can like below to retrieve name
Package^ package = Package::Current;
PackageId^ packageId = package->Id;
String^ name= packageId->Name ;
But the above code is just return name of Package, if your exe file name is different with package name, above code is return incorrect name.
So you must use some code like below (based on How do I get the name of the current executable in C#?)
char filename[MAX_PATH];
DWORD size = GetModuleFileNameA(nullptr, filename, MAX_PATH);
std::reverse_iterator<char*> it = std::find_if(std::rbegin(filename), std::rend(filename), [](const char c) { return c == '\\'; });
const std::string exeName(it.base());

Related

Using C++ dll file need to convert jpg into Webp using nodejs

I've searched stackoverflow and numerous websites for an answer to this. But I can't find any proper information regarding this. I am stuck.
I want to convert jpg into Webp by Using C++ dll file in nodejs. For this i Installed FFi Also, But getting error in Response. Can you Please Check my Code.
const ffi = require('ffi');
const ref = require('ref');
const fs = require('fs');
var int = ref.types.int ;
const dllFile = './WebpSdk.dll';
if (!fs.existsSync(dllFile)) {
throw (new Error('dll does not exist'));
}
const lib = ffi.Library(dllFile, {
'SelectSingleFile': [ 'bool', [ref.types.Object,ref.types.int] ]
});
const filepath ='./thumb/file.jpg';
console.log(lib.SelectSingleFile(filepath,1));//1 is FileType Like jpg
Getting this Error.
[nodemon] app crashed - waiting for file changes before starting...
C++ Dll Function SelectSingleFile Role.
//Select single file
BOOL bCheck = SelectSingleFile(PathName, ImageType);//SDK function
If bcheck true file is valid if false file is invalid.

Edge First Tab Error when Opening via System()

When doing
const std::string LaunchStr = "C:\\\"Program Files (x86)\"\\Microsoft\\Edge\\Application\\msedge.exe --profile-directory=\"Profile 1\" C:\\Users\\redacted1\\redacted4.html";
System(LaunchStr.c_str());
Microsoft Edge launches as expected, the loaded profile is the correct one and there is a new tab on redacted4.html. However, the first tab (and the focused one too) is the following url program%20--fast-start%20files%20%28x86%29/Microsoft/Edge/Application/msedge.exe. Which I find weird because nowhere in my code do I write program%20--fast-start%20files%20%28x86%29/.
Why is that? How can I prevent it?
I suggest you try to refer to the sample code below that may help you to launch the MS Edge browser correctly with the correct profile and with the specified URL.
#include <windows.h>
int main()
{
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
SHELLEXECUTEINFOW sei = { sizeof sei };
sei.lpVerb = L"open";
sei.lpFile = L"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe";
sei.lpParameters = L"--user-data-dir=\"C:\\Users\\<user>\\AppData\\Local\\Microsoft\\Edge\\User Data\\Profile 1\" C:\\Users\\redacted1\\redacted4.html"; // Modify the path for user-profile here...
ShellExecuteExW(&sei);
}
Note: You can type edge://version/ in the address bar of the Edge browser and see the profile path to modify it in the above code sample.
Output:

How do I get username and appname for file path

How am I able to define a path like "C:/Users/<USER>/AppData/Local/<APPNAME>", for different username and app? How do I set this to automatically get the user and the appname? Thank you.
You can use SHGetKnownFolderPath to get the full path of App Local:
...
#include <KnownFolders.h>
#include <ShlObj.h>
...
SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_SIMPLE_IDLIST, NULL, &path); // NULL for current user
...
To get the Local AppData path for a given user, use SHGetFolderPath() specifying CSIDL_LOCAL_APPDATA, or SHGetKnownFolderPath() specifying FOLDERID_LocalAppData. Both take an optional user token for the desired user account to query. If you don't provide a token, the user associated with the calling thread is used.
To get the username:
char username[MAX_PATH];
DWORD size = MAX_PATH;
GetUserName(username,&size);
To get the appname(Executable File Name without ".exe"):
char appname[MAX_PATH];
char buffer[MAX_PATH];
GetModuleFileName(NULL, appname,MAX_PATH); //get the string: "PATH\\appname.exe"
char *szExe = NULL;
//Remove prefix
GetFullPathName(appname, MAX_PATH, buffer, &szExe);
//Remove suffix
strncpy_s(appname, szExe, strlen(szExe) - strlen(".exe"));

Setting the name of a text file to a variable in qt

I'm exporting data to a text file in qt every time a run a code. With my current code that file is overwritten each time. My question is how can I set the title to be a variable eg pulse_freq, this way new files will be created based on my variable values. I just can't get the syntax right.
Is there a way to put my files in a folder in the same directory as my build files? I need my code to be cross platform and if I use the full path name it's apparently incompatible with any non-windows OS. If I just name the files there'd be too much clutter in the folder. Relevant code is below:
// Export to data file
QString newname = QString::number(variables.nr_pulses);
QString filename = "C:/Users/BIC User/Documents/BIC Placement Documents/QT_data/Data.txt";
QFile file( filename );
You can just use something along lines:
QString s1 = "something";
QString s2 = " else";
QString s3 = s1 + s2;
QString concatenation with overloaded operator+ works like charm.
And about referencing folder you're in, instead of hardcoding its path, use QDir::currentPath()
Thus, your filename creation should look like the following:
QString folder = QDir::currentPath();
QString file = QString::number(variables.nr_pulses); //or whatever else you want it to be
QString extension = ".txt" // or whatever extension you want it to be
QString full_filename = folder + file + extension;
In order not to mess with appending string after the extension, just separate it into another QString and concatenate those 3 elements as above (folder + file + extension).

Non-documented API & headers

I'm using JavaScriptCore in one of my Objective-C project, and I'd like to know at any time what's the current file & line when falling into a JS callback.
There is no way to do it with the public headers, so I took a look at the sources and it seems possible to access the file & line by using some C++ code.
// ctx is a JSContextRef, that's the only type I have an access to
JSC::JSValue jsCtx = toJS(ctx);
CodeBlock* codeBlock = jsCtx->codeBlock();
// Line
unsigned sourceOffset = codeBlock->sourceOffset();
// Source URL
SourceProvider* sourceProvider = codeBlock->source();
const String& url = sourceProvider->url();
It obviously requires the definitions of JSC, JSValue, CodeBlock, and SourceProvider. I have all these in separate headers, but it's really massive.
Should I directly include those headers?
What if those files require other headers? I might end by having multiple headers that I won't use.
Let's forget that the internal source code might change: is what I want to do even possible?
You can know this information from the public Headers itself. Here you go:
When the exception occurs, the exception object contains the following keys:
line, sourceId, sourceURL, name, message
You can access the values for these keys to find in which file (sourceURL) and in which line number (line) the exception has occurred.
Example:
JSObjectRef exceptionObj = JSValueToObject(context, exception, NULL);
//Convert the exceptionObj into dictionary (I leave the implementation of this to you..)
NSDictionary *exceptionDict = [self convertJSObjectToDictionary:exceptionObj];
NSString *lineNumber = [exceptionDict objectForKey:#"line"];
NSString *fileName = [exceptionDict objectForKey:#"sourceURL"];
NSLog(#"Exception has occurred in file:%# at line number:%#", fileName, lineNumber);
Hope this helps!
~ Sunil Phani Manne