Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
V

Valentinor

@Valentinor
About
Posts
129
Topics
38
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How to use JNI without setting Environment Variables
    V Valentinor

    jschell wrote:

    1. Lay down the files into the file system.

    Yes or no, depending what you mean by "file system". When the app is installed, inside it's folder structure it will also have Eclipse Adoptium (...\MyApp\ThirdParty\Eclipse Adoptium\jre-17.0.7.7-hotspot)

    jschell wrote:

    2. Create the registry data so the user can uninstall it.

    It will be for the app itself and not just for java, as Eclipse Adoptium will be a part of the app, integrated into it, and not separate. When the app is uninstalled, so will Eclipse Adoptium as it is part of its files. If the user has any other form of Java installed, they won't be affected in any way.

    jschell wrote:

    So apparently what you are actually doing is giving the user the option to use the Java that is already installed or to use the VM that comes with your application.

    No. The app will only use Java that comes with it, as the app won't even look in another location if Java is installed, only inside it's file structure.

    jschell wrote:

    whether the VM they have installed is the correct version for your appliction

    I explained in the above statement why this doesn't matter for my case.

    jschell wrote:

    As a suggestion only you might want to work on your error handling if it turns out Java isn't installed. Telling a user that a dll is missing is not going to help them nor you when they ask why it isn't working.

    I never said that I don't have an error handling system already added. If any of Java files are missing or are corrupted, or any of the other app files are missing or are corrupted, an error will appear telling the user to run the check for corrupted files or reinstall the app, accompanied by an error message, that will tell me exactly which file is the problem, if it is missing or if it is corrupted.

    Java c++ java com sysadmin tutorial

  • How to use JNI without setting Environment Variables
    V Valentinor

    jschell wrote:

    That is oddly phrased since they will need to have java installed somehow.

    Why did you stopped there with the quote? As the next part of the phrase contained the reason why I'm not making the install Java:

    Valentinor wrote:

    but it comes in the app files with a license agreement, I'm using Eclipse Adoptium (...\MyApp\ThirdParty\Eclipse Adoptium\jre-17.0.7.7-hotspot).

    So yeah, they don't need to install java, as it is included with the app.

    Java c++ java com sysadmin tutorial

  • How to use JNI without setting Environment Variables
    V Valentinor

    I managed to make it work:

    #include
    #include
    #include
    #include
    #include
    #include

    JavaVM* jvm = nullptr;

    std::string createVM(std::string location) {
    std::string jvmLocation = location;
    jvmLocation.append("ThirdParty\\Eclipse Adoptium\\jre-17.0.7.7-hotspot\\bin\\servers\\jvm.dll");
    HMODULE hJVMDLL = LoadLibraryA(jvmLocation.c_str());
    typedef jint(JNICALL* fpCJV)(JavaVM**, void**, void*);
    if (hJVMDLL != NULL) {
    fpCJV JNI_CreateJavaVM = (fpCJV)::GetProcAddress(hJVMDLL, "JNI_CreateJavaVM");
    location.insert(0, "-Djava.class.path=");
    location.append("Data\\Java");
    JavaVMInitArgs vm_args;
    JavaVMOption* options = new JavaVMOption[1];
    options[0].optionString = &location[0];
    vm_args.version = JNI_VERSION_10;
    vm_args.nOptions = 1;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = false;
    JNIEnv* env = nullptr;
    jint rc = JNI_OK;
    if (jvm == nullptr) {
    rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    }
    else {
    rc = jvm->AttachCurrentThread((void**)&env, NULL);
    }
    delete[] options;
    if (rc != JNI_OK) {
    if (rc == JNI_EVERSION)
    return "JNI_EVERSION";
    else if (rc == JNI_ENOMEM)
    return "JNI_ENOMEM";
    else if (rc == JNI_EINVAL)
    return "JNI_EINVAL";
    else if (rc == JNI_EEXIST)
    return "JNI_EEXIST";
    else
    return "JNI_FATALERROR";
    }
    return "JNI_CREATED";
    }
    else {
    return "ERROR_LOADING_DLL";
    }
    }

    std::string createIdentification() {
    JNIEnv* env;
    jvm->AttachCurrentThread((void**)&env, NULL);
    jclass jClass = env->FindClass("JavaMethods");

    if (jClass == nullptr) {
    	return "ClassNotFound cI";
    }
    else {
    	jmethodID methodID = env->GetStaticMethodID(jClass, "createIdentification", "()Ljava/lang/String;");
    	if (methodID == nullptr) {
    		return "MethodNotFound cI";
    	}
    	else {
    		jboolean isCopy;
    		jstring jResult = (jstring)env->CallStaticObjectMethod(jClass, methodID);
    		const char\* string = env->GetStringUTFChars(jResult, &isCopy);
    		std::string result = string;
    		env->ReleaseStringUTFChars(jResult, string);
    
    		return result;
    	}
    }
    

    }

    int main() {
    std::cout << createVM("Location_To_App_Folder").c_str() << std::endl;
    if (jvm != NULL) {
    std::cout << createIdentification().c_str();
    }

    std::this\_thread::sleep\_for(std::chrono::milliseconds(2000));
    return 0;
    

    }

    Thank you @Richard-MacCutchan for the or

    Java c++ java com sysadmin tutorial

  • How to use JNI without setting Environment Variables
    V Valentinor

    I found the following code posted by @Richard-MacCutchan to a thread about how to use JNI without having to add the location of jvm.dll into Environment Variables. I kinda understood how it works but still...

    /*
    ******************************************************************************
    *
    * Name : cpptojava.cpp
    *
    * Function : Basic Console application to call a Java class.
    *
    * Created : 09 Jun 2022
    *
    ******************************************************************************
    */

    #include
    #include
    #include
    #include
    #include
    #include // requires -std:c++17

    #include "jni.h"

    //
    // Find the path to the jvm.dll given the base path to the Java runtime (or JDK if installed)
    //
    HMODULE LoadDll(
    const char* jrePath
    )
    {
    const char* pszjvmname = "bin\\server\\jvm.dll"; // Java VM name
    HMODULE hJVMDLL = 0;

    std::filesystem::path jvmpath(jrePath);
    jvmpath /= pszjvmname;
    if (!std::filesystem::exists(jvmpath))
    {
        std::cerr << "JVM library " << jvmpath << " not found." << std::endl;
        // throw an exception 
    }
    
    // load the Java VM DLL into our address space
    // NB filesystem::path is Unicode
    hJVMDLL = LoadLibraryW(reinterpret\_cast(jvmpath.c\_str()));
    if (hJVMDLL != NULL)
    {
        std::clog << "jvm.dll loaded from: " << jvmpath << std::endl;
    }
    return hJVMDLL; // a Windows HANDLE to the DLL
    

    }

    //
    // A function to load the Java VM and initialise the JNI interface
    // There are a number of diagnostic messages to show progress
    //
    JNIEnv* AttachJVM(
    const char* jrePath,
    JavaVM* jvm,
    std::string strcwd
    )
    {
    HMODULE hJVMDLL = LoadDll(jrePath);
    if (hJVMDLL == 0)
    {
    return nullptr;
    }
    typedef jint(JNICALL* fpCJV)(JavaVM**, void**, void*);
    fpCJV JNI_CreateJavaVM = (fpCJV)::GetProcAddress(hJVMDLL, "JNI_CreateJavaVM");

    // tell the JVM where to find the class
    JavaVMOption    options\[2\];
    std::stringstream ssoptions;
    ssoptions << "-Djava.class.path=";
    ssoptions << strcwd;
    std::string stropts = ssoptions.str();
    options\[0\].optionString = const\_cast(stropts.c\_str());
    options\[1\].optionString = const\_cast("-verbose:jni");   // print JNI-related messages
    
    JavaVMInitArgs  vm\_args; // JDK/JRE 6 VM initialization
    
    Java c++ java com sysadmin tutorial

  • Using JNI without having to add jvm.dll path inside Environment Variables (User or System)
    V Valentinor

    Hi, I stumbled upon this thread and I like what I see here, and it is something I could use in some of my projects, which is to use JNI without having to add jvm.dll location into Environment Variables. But two minor problem, I get lost in all of that code to change it for my use case :), and also, on client machines I'm not making them install Java, but it comes in the app files with a license agreement, I'm using Eclipse Adoptium (...\MyApp\ThirdParty\Eclipse Adoptium\jre-17.0.7.7-hotspot). If it isn't much to ask, could you change the above code so that it will work in this scenario: Before creating the JVM, I'm running some other functions, and one of them returns the location for the app folder (ex: D:\Program Files\MyApp). Inside that folder are the apps data, and a folder Data, inside it a folder Java, and inside that all the java class files (...\MyApp\Data\Java\JavaMethods.class). So because I know the location of the app, I can give the location for jvm.dll by using the variable std::string location which would have D:\Program Files\MyApp, then to that I can append \ThirdParty\Eclipse Adoptium\jre-17.0.7.7-hotspot\bin\server for the location of jvm.dll. To be more precise, what I need to add before location.insert(0, "-Djava.class.path=");, to load the jvm.dll knowing it's location by combining like I said, the value from function parameter which would be set before calling the function and would have as an example D:\Program Files\MyApp with \ThirdParty\Eclipse Adoptium\jre-17.0.7.7-hotspot\bin\server. Here is an example how my app is using JNI extracted to a new project:

    #include #include #include #include #include JavaVM* jvm = nullptr;

    std::string createVM(std::string location) {
    //Here what I need to add to load the dll from location + ThirdParty\Eclipse Adoptium\jre-17.0.7.7-hotspot\bin\server
    location.insert(0, "-Djava.class.path=");
    location.append("Data\\Java");
    JavaVMInitArgs vm_args;
    JavaVMOption* options = new JavaVMOption[1];
    options[0].optionString = &location[0];
    vm_args.version = JNI_VERSION_10;
    vm_args.nOptions = 1;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = false;
    JNIEnv* env = nullptr;
    jint rc = JNI_OK;
    if (jvm == nullptr) {
    rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    }
    else {
    rc = jvm->AttachCurrentThread((void**)&env, NULL);
    }
    delete[] options;
    if (rc

    Java c++ java question workspace

  • GetVCPFeatureAndVCPFeatureReply fails when called
    V Valentinor

    I'm going to look for other ways to change gamma and brightness as this function has too many limitations on hardware. In the past I used SetDeviceGammaRamp[^] for changing gamma, but now it seems that function shouldn't be used anymore. If anyone has some suggestion, they are welcomed and helpful.

    C / C++ / MFC com json question

  • GetVCPFeatureAndVCPFeatureReply fails when called
    V Valentinor

    CPallini wrote:

    On function call failure, why didn't you call GetLastError, as hinted by the documentation[^]?

    I don't know how to use it. C/C++ isn't my main language, and only use it when I have to.

    CPallini wrote:

    Also, at the very beginning of the linked page, there is:

    That is not a problem for my monitor.

    C / C++ / MFC com json question

  • GetVCPFeatureAndVCPFeatureReply fails when called
    V Valentinor

    I was trying to get gamma value of monitor using GetVCPFeatureAndVCPFeatureReply[^] to have the default value, and later when using SetVCPFeature[^] to be able to revert to it. But when GetVCPFeatureAndVCPFeatureReply is called, it fails. What am I doing wrong? Here is the code test:

    #include
    #include
    #include
    #include
    #include

    #pragma comment(lib, "Dxva2.lib")

    int main() {
    // Time to click on a different window to test on multiple monitors
    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    HWND hwnd = GetForegroundWindow();

    HMONITOR hMonitor = NULL;
    DWORD cPhysicalMonitors;
    LPPHYSICAL\_MONITOR pPhysicalMonitors = NULL;
    
    hMonitor = MonitorFromWindow(hwnd, MONITOR\_DEFAULTTONEAREST);
    
    BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);
    if (bSuccess) {
    	pPhysicalMonitors = (LPPHYSICAL\_MONITOR)malloc(cPhysicalMonitors \* sizeof(PHYSICAL\_MONITOR));
    	if (pPhysicalMonitors != NULL) {
    		bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);
    		if (bSuccess) {
    			DWORD pdwCurrentValue;
    			DWORD pdwMaximumValue;
    			bSuccess = GetVCPFeatureAndVCPFeatureReply(pPhysicalMonitors->hPhysicalMonitor, 0x72, NULL, &pdwCurrentValue, &pdwMaximumValue);
    			if (bSuccess) {
    				std::cout << "Current value: " << pdwCurrentValue << " | MaximumValue: " << pdwMaximumValue;
    			}
    			else {
    				std::cout << "Failed GetVCPFeatureAndVCPFeatureReply" << std::endl;
    			}
    		}
    		else {
    			std::cout << "Failed GetPhysicalMonitorsFromHMONITOR" << std::endl;
    		}
    
    		DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);
    		free(pPhysicalMonitors);
    	}
    	else {
    		std::cout << "Is null" << std::endl;
    	}
    }
    else {
    	std::cout << "Failed GetNumb
    
    C / C++ / MFC com json question

  • [Solved] Getting and setting a File's all attributes
    V Valentinor

    I found a solution for the information when you open the Properties of a file, from the General tab, as for Details tab, that info it is copied with the file. Example for General tab:

    DosFileAttributes dos = Files.readAttributes(original.toPath(), DosFileAttributes.class);
    DosFileAttributeView dosView = Files.getFileAttributeView(copy.toPath(), DosFileAttributeView.class);
    dosView.setArchive(dos.isArchive());
    dosView.setHidden(dos.isHidden());
    dosView.setReadOnly(dos.isReadOnly());
    dosView.setSystem(dos.isSystem());
    dosView.setTimes(dos.lastModifiedTime(), dos.lastAccessTime(), dos.creationTime());

    Java question java html oracle com

  • [Solved] Getting and setting a File's all attributes
    V Valentinor

    This doesn't look all that hard to do in C++, which makes me believe that there might be a way to do it in Java as well. If it was locally only (on the same machine) then I could make a dll and copy the attribues that way as both the backup and restored files are on the same machine, but the app also has the Socket part and sending the C++ attributes might give a a headache when trying to crate the code, compared to just sending java objects. Even so, this is good info, thanks!

    Java question java html oracle com

  • [Solved] Getting and setting a File's all attributes
    V Valentinor

    I made an app (Windows only) with which you can create a backup of a folder/file locally or to a server. I'm done with the copy/restore process, but when it comes to restoring I have a small technical problem. If the file in originally hidden, then this is easy as you can call Files.setAttribute(file.toPath(), "dos:hidden", true); which will make the file hidden on client side after it is created/restored, but there are files like desktop.ini which aren't simply hidden but are system protected hidden and the above code will only give it the hidden status, and not system protected hidden. There is also Files.getAttribute() but as the name suggests it only returns the attribute value for a single attribute, and not all of them to easily be able to do Files.setAttributes(file.toPath(), Files.getAttributes(file.toPath()));. So my question is, is there a way to get a list will all the attributes a file has and then setting them in a more dynamically way and not having to call Files.getAttribute(); / Files.setAttribute(); for each of them line by line? If unfortunately there isn't one, and you do have to set them line by line, can someone give me a list with all the attributes for Windows OS like there is here for example with DateTimeFormatter[^] for pattern symbols?

    Java question java html oracle com

  • How to get disk model and serial number for the disk Windows is installed on
    V Valentinor

    Richard MacCutchan wrote:

    So the error message "Description = Invalid query" is telling you that the command is in error, and you need to find out what the correct format is.

    I did make it work, for some reason I had to use a lot of \ in C++ (I thought you don't need to escape \ in C++, but maybe that is valid in some cases, or maybe I'm just wrong about it). If used in CMD directly then you need \\\\.\\ I'm still looking for a native use for wmi as that is suggested when used with C++, but at least if I don't find one, you can use this now that is working.

    std::cout << exec("wmic diskdrive where DeviceID='\\\\\\\\.\\\\PHYSICALDRIVE3' get model,serialnumber");

    //Edit: I'm not looking anymore. I found out that in C++ you need a lot more code to make it work compared with .NET, so I guess you can stick with this, now that it is working.

    C / C++ / MFC help c++ database data-structures tutorial

  • Windows Files.copy Folder custom icon
    V Valentinor

    After you use Files.copy(What to copy, Where to copy, StandardCopyOption.REPLACE_EXISTING); to copy a folder to a new location it loses it's custom icon as that info is stored in the file desktop.ini inside the folder. Now if you copy that file also inside it after, it still doesn't changes the icon. So how can you "tell the folder to use the desktop.ini" to apply the icon? Or how else can you copy a folder and keep it's custom icon as well?

    Java question

  • JavaFX multithreading operation changing GUI elements
    V Valentinor

    I get the error when I'm calling .setText(). Everything related to the GUI must run on FX thread, but if you are running something on that thread, well until it is finished the interface is frozen.

    Java help question announcement

  • Client can't connect to server when client/server are different projects
    V Valentinor

    OK, so there isn't something to specifically tell it, like socket.ImDone(), but in the context they speak to each other. In that case, I'm using something like that in main project. The example in main thread was something simple, to only address java.net.ConnectException: Connection timed out problem, which was in fact caused by antivirus firewall.

    Java help question csharp java sysadmin

  • Client can't connect to server when client/server are different projects
    V Valentinor

    Then how this signalling should be done? In that simple example the server was only expecting to receive a single object and then it knew it could close because that was all. And on client side the same thing, it knew it only had to send an object and it wasn't goin to receive anything back, so it could close it as that was all.

    Java help question csharp java sysadmin

  • Client can't connect to server when client/server are different projects
    V Valentinor

    I found the problem, and it was because the antivirus was blocking network not filtered. I reinstalled it and forgat to add the rule again. But that being said, why was it working when it was in the same class? Was it automatically using localhost in that situation even tho it had a specific IP given?

    Java help question csharp java sysadmin

  • Client can't connect to server when client/server are different projects
    V Valentinor

    I was doing some Socket tests, and while using localhost everything ran as I wanted. But then I tried to switch it to IP address, and well then things got weird. While the code was in the same class (Code_1 bellow), the simplified test from below was running fine, but as soon as I moved the server code in another project, even another package or class in same package, then when I'm running client code I'm getting java.net.ConnectException: Connection timed out: connect. If when the server is in another project, and for client I'm switching to localhost, then this time it is working. It doesn't want to work when they are in different projects/packages, and the client is using IP address and not localhost, but they work if both are in same project even if client has IP address or localhost.

    public static void main(String\[\] args) throws Exception {
    	Thread thread = new Thread(new Runnable() {
    		@Override
    		public void run() {
    			try {
    				ServerSocket server = new ServerSocket(port);
    				Socket socket = server.accept();
    				System.out.println(new ObjectInputStream(socket.getInputStream()).readObject());
    				socket.close();
    				server.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (ClassNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	});
    	thread.start();
    
    	Socket socket = new Socket("IP\_ADDRESS", port);
    	new ObjectOutputStream(socket.getOutputStream()).writeObject("Test");
    	socket.close();
    }
    

    What is the problem, and what can I do to fix it?

    Java help question csharp java sysadmin

  • Memory usage for requested rows from database
    V Valentinor

    jschell wrote:

    That is not how the jdbc is defined to work.

    OK, good. I'm the only developer for now, so I'm working on everything, and DB/SQL isn't something I'm good at, but I don't want to make a bad DB when it comes to performance.

    jschell wrote:

    But a bad driver could definitely do that.

    I'm using Oracle JDBC driver 11, so I would say that is out of the question.

    jschell wrote:

    The result should be paged.

    Is this a good example of paging?[^] Unfortunately, that isn't something I know of.

    jschell wrote:

    You should NEVER, for example, allow for a design where a UI could expect a user to view 'millions' of rows.

    Oh, no way that. By that I meant something else, but you answered it here:

    jschell wrote:

    That is not how the jdbc is defined to work. But a bad driver could definitely do that.

    Thank you for all the info!

    Java java database regex performance question

  • Memory usage for requested rows from database
    V Valentinor

    When you are running in Java:

    ResultSet rs = stmt.executeQuery("SELECT Column FROM Table WHERE Condition");

    Does the database create in memory a list with all the rows/values that have the given condition, and it is keeping that list until the rs is close? Or it will only keep the first value, and move to the next value when rs.next() is called? I'm asking this because you can give it the command to FETCH NEXT X ROWS ONLY, and in case you have a database with millions of entries, that would save time and resources when you only want X rows, and not all of them that match the given condition. Or the FETCH is only a hard limit to know when to stop with sending the rows/values one by one?

    Java java database regex performance question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups