Did I properly used jni wrapper here?

  • Thread starter Thread starter BataBo Jokviu
  • Start date Start date
B

BataBo Jokviu

Guest
So to me jni is pretty hard and confusing but regardless I've written I used it and all I want to know is this code properly written if not where do I fix it?

javaexport.cpp

// Save as "HelloJNI.c"
#include <jni.h> // JNI header provided by JDK
#include <stdio.h> // C Standard IO Header
#include "javaexport.h" // Generated
#include <curl\curl.h>
using namespace std;
#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
#include <comutil.h>
#include <fstream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "wbemuuid.lib")


size_t CurlWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, std::string *s)
{
size_t newLength = size * nmemb;
try
{
s->append((char*)contents, newLength);
}
catch (std::bad_alloc &e)
{
//handle memory problem
return 2;
}
return newLength;
}
// Implementation of the native method sayHello()
JNIEXPORT jint JNICALL Java_javaexport_JAuthenticateRFF(JNIEnv *env, jobject thisObj) {


string strFromBstr;
char key[200];
fstream file;
file.open("key.dat", ios::out | ios::in);
file >> key;
file.close();



HRESULT hres;

// Initialize COM.
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
return 2; // Program has failed.
}


// Initialize
hres = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);


if (FAILED(hres))
{
CoUninitialize();
return 2; // Program has failed.
}
// Obtain the initial locator to Windows Management
// on a particular host computer.
IWbemLocator *pLoc = 0;

hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *)&pLoc);

if (FAILED(hres))
{
CoUninitialize();
return 2; // Program has failed.
}
IWbemServices *pSvc = 0;

// Connect to the root\cimv2 namespace with the
// current user and obtain pointer pSvc
// to make IWbemServices calls.

hres = pLoc->ConnectServer(

_bstr_t(L"ROOT\\CIMV2"), // WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pSvc // IWbemServices proxy
);

if (FAILED(hres))
{

CoUninitialize();
return 2; // Program has failed.
}


// Set the IWbemServices proxy so that impersonation
// of the user (client) occurs.
hres = CoSetProxyBlanket(

pSvc, // the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // client identity
EOAC_NONE // proxy capabilities
);

if (FAILED(hres))
{

pSvc->Release();
pLoc->Release();
CoUninitialize();
return 2; // Program has failed.
}

// Use the IWbemServices pointer to make requests of WMI.
// Make requests here:

// For example, query for all the running processes
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t(L"SELECT VolumeSerialNumber from Win32_LogicalDisk where DeviceID = 'c:'"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{

pSvc->Release();
pLoc->Release();
CoUninitialize();
return 2; // Program has failed.
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;

while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);

if (0 == uReturn)
{
break;
}

VARIANT vtProp;

// Get the value of the Name property
hres = pclsObj->Get(L"VolumeSerialNumber", 0, &vtProp, 0, 0);
VariantClear(&vtProp);

strFromBstr = (const char*)_bstr_t(V_BSTR(&vtProp));


pclsObj->Release();
pclsObj = NULL;
}

}

// Cleanup
// ========

pSvc->Release();
pLoc->Release();
pEnumerator->Release();

CoUninitialize();

std::ostringstream oss;
oss << "[REDACTED]" << key << "[REDACTED]" << strFromBstr;
std::string var = oss.str();

std::string zlj;

CURL *curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_ALL);


curl = curl_easy_init();
if (curl) {
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "Accept: */*");
chunk = curl_slist_append(chunk, "User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; Rigor | The Leader in Digital Experience Lifecycle Management)");
chunk = curl_slist_append(chunk, "Host: [REDACTED]");
chunk = curl_slist_append(chunk, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &zlj);
curl_easy_setopt(curl, CURLOPT_URL, "[REDACTED]");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, var.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
return 2;
}
/*fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res))*/


curl_easy_cleanup(curl);
}

curl_global_cleanup();


int StatusCode;

if (zlj.find("error")) {
return 1;
}
else {
return 0;
}



return 1;
}

javaexport.h

#pragma once
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloJNI */

#ifndef _Included_javaexport
#define _Included_javaexport
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_javaexport_JAuthenticateRFF(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Continue reading...
 
Back
Top