DLLから情報(キー)を抽出する小さなCライブラリを作成しました。JNA:無効なメモリアクセス
GetKey.h
#pragma once
#include <stdint.h>
#define getKey(x) getKeyL(x)
#define getKey(x,y) getKeyP(x,y)
void __declspec(dllexport) getKeyP(char *path, uint8_t key[]);
void __declspec(dllexport) getKeyL(uint8_t key[]);
GetKey.c
#include "getkey.h"
#include <stdio.h>
#include <string.h>
#include <windows.h>
void __declspec(dllexport) getKeyP(char *path, uint8_t key[])
{
int lib = LoadLibraryA((path != NULL) ? path : "myLib");
((void(*)(void))(lib + 0x1340))();
lib += 0x14020;
for (int i = 0; i < 8; ++i)
{
key[i] = *(uint8_t*)(lib + (i << 4));
}
FreeLibrary(lib);
}
void __declspec(dllexport) getKeyL(uint8_t key[])
{
getKeyP(NULL, key);
}
そうのような
私のJavaバインディングルックス:私はgetKeyP
とgetKeyL
を起動するたびに、私はスローされた例外を取得し
public class GetKey
{
private static final GetKeyBinding INSTANCE;
static
{
System.setProperty("jna.library.path", "lib/dll/");
GetKeyBinding lib;
try
{
lib = Native.loadLibrary("x86_64/GetKey.dll", GetKeyBinding.class);
}
catch (UnsatisfiedLinkError e)
{
try
{
lib = Native.loadLibrary("i386/GetKey.dll", GetKeyBinding.class);
}
catch (UnsatisfiedLinkError f)
{
System.err.println("Failed to load library");
lib = null;
}
}
INSTANCE = lib;
}
public static void getKey(byte[] path, byte[] key)
{
INSTANCE.getKeyP(path, key);
}
public static void getKey(byte[] key)
{
INSTANCE.getKeyL(key);
}
private static interface GetKeyBinding extends Library
{
void getKeyP(byte[] path, byte[] key);
void getKeyL(byte[] key);
}
}
私はパラメータを変更しようとしましたbyte[]
からPointer
とByteBuffer
のタイプもありますが、私もextend
ing StdCallLibrary
を試しました(当初はあまり意味がありませんでしたが、試しても痛いことはありませんか?)。
#include <stdio.h>
#include "GetKey.h"
int main()
{
uint8_t key[8];
getKeyP(NULL, key);
for (int i = 0; i < 8; ++i)
{
printf("%02X ", key[i]);
}
getchar();
return 0;
}
を任意の助けを喜んで高く評価され ...:私はそれのために小さなテストアプリケーションを書いたので、私は、コードが動作する知っています!私はそうのように、Javaから私の関数を呼び出してい
:
編集私のDLLとデバッグにデバッガをアタッチした後
byte[] key = new byte[8];
GetKey.getKey(key);
あなたも、あなたが呼んでいる方法を含めることができます: - : 私は私のJavaコード変更することで、これを固定
をそして意図したとおり、今では動作します(。 Javaの関数ですか? – cubrr
その場合NullPointerExceptionが発生しないでしょうか?コードがDLLのロードを試みる順序を変更しようとしましたが、もちろん、32bit DLLであり、もう1つは64bit代替です(または、おそらく私はここで完全にオフになっています) – user3320529
'lib == 0'なら' 0x1340'で関数を呼び出そうとしています。セグメンテーションフォールトや不正なメモリアクセスが起こるでしょう。 – cubrr