Monday, December 15, 2014

An Introduction to JNI using a Bottom Up Approach: Step 1

Continued from An Introduction to JNI using a Bottom Up Approach: Prologue.

Step 1: C++ Low Level Function

  1. In your working directory, add an empty file, named "PrintOpenWindows.h". This will make the functions available other C/C++ files.
    #ifndef _PRINT_OPEN_WINDOWS_H
    #define _PRINT_OPEN_WINDOWS_H

    #ifdef __cplusplus
            extern "C" {
    #endif
            void printOpenWindowsEntryPoint ();
    #ifdef __cplusplus
            }
    #endif

    #endif
  2. In your working directory, add the following code, in a file named "PrintOpenWindows.cpp":
    #include "PrintOpenWindows.h"
    #include <stdlib.h>
    #include <string.h>
    #include <tchar.h>
    #include <windows.h>
    #include <iostream>

    using namespace std;

    BOOL CALLBACK FindWindowBySubstr(HWND hwnd, LPARAM substring)
    {
        const DWORD TITLE_SIZE = 1024;
        TCHAR windowTitle[TITLE_SIZE];

        if (GetWindowText(hwnd, windowTitle, TITLE_SIZE))
        {
            _tprintf(TEXT("%s\n"), windowTitle);
            // Uncomment to print all windows being enumerated
            if (_tcsstr(windowTitle, LPCTSTR(substring)) != NULL)
            {
                // We found the window! Stop enumerating.
                return false;
            }
        }
        return true// Need to continue enumerating windows
    }

    void printOpenWindowsEntryPoint()
    {
        cout << "PRINT_OPEN_WINDOWS ENTRY START" << endl;
        const TCHAR substring[] = TEXT("Substring");
        EnumWindows(FindWindowBySubstr, (LPARAM)substring);
        cout << "PRINT_OPEN_WINDOWS ENTRY FINISH" << endl;
    }

    int main()
    {
        cout << "PRINT_OPEN_WINDOWS MAIN START" << endl;
        printOpenWindowsEntryPoint();
        cout << "PRINT_OPEN_WINDOWS MAIN FINISH" << endl;
    }
  3. Compile the code and run "PrintOpenWindows.exe", using the following commands:
    cd /D "C:\Users\Scott\workspace\myrepo\my-app\src\main\resources\dlls"

    "C:\MinGW64\bin\g++.exe" -o PrintOpenWindows.exe PrintOpenWindows.cpp

    PrintOpenWindows.exe
Continued at An Introduction to JNI using a Bottom Up Approach: Step 2.

This post was reposted from http://scottizu.wordpress.com/2013/08/21/an-introduction-to-jni-using-a-bottom-up-approach-step-1/, originally written on August 21st, 2013.

1 comment:

  1. The printOpenWindowsEntryPoint function needs to be defined before main so step 3 might not work. If so, add the code from step 4 and then compile in step 5. Step 3 should have "-o" with no spaces and there are 3 commands listed...

    ReplyDelete