Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JNI и DLL

11 views
Skip to first unread message

Vadim Markin

unread,
Mar 13, 2008, 4:05:53 AM3/13/08
to
Есть некий Java класс и есть dll'ка. Для простоты напишем вот так:

Java:

public class JniTest {

static {
System.loadLibrary("library");
}

public static native int method1();
public static native String method2();
public static native boolean method3();

}

DLL:

JNIEXPORT jint JNICALL Java_com_pembroke_lni2_util_Jnitest_method1 (JNIEnv *,
jclass) {
...
}

и т.д.

И где-то в Java коде - в одном и том же классе все методы неоднократно
дёргаются статически - JniTest.method1() - и т.д.

Так вот - логгинг DLL показывает что DllMain вызывается огромную кучу раз. Я в
DLL не силён, но я так понимаю, что DllMain дёргается именно при первоначальной
загрузке DLL. Т.е., выходит, DLL грузится вот это огромное число раз. Хотя, по
идее - static всё-таки - должна грузиться единожды при загрузке класс JniTest.

Есть ли таки способ загрузить DLL раз и навсегда, и потом только дёргать её
функции? И, как следствие, важно ещё запоминать на стороне DLL состояние
объектов между вызовами. Т.е., я, например, в DllMain создал экземпляр некоего
сишного класса, а при следующих вызовах он уже тот же самый висит по-прежнему в
памяти, и я продолжаю работать с его членами. Постоянная же загрузка/выгрузка
DLL этому препятстсвует.

Может что в Java коде некошерно написано?

Если это важно - Java 1.4.


--
Vadim

Vadim Markin

unread,
Mar 13, 2008, 4:52:20 AM3/13/08
to
All,

13 Mar 08, Vadim Markin wrote to All:
VM> DLL:

VM> JNIEXPORT jint JNICALL Java_com_pembroke_lni2_util_Jnitest_method1
VM> (JNIEnv *, jclass) {

Тут разумеется надо читать:

JNIEXPORT jint JNICALL Java_Jnitest_method1 (JNIEnv *, jclass) {

либо ещё имя пакета там и там прописывать (про UnsatisfiedLinkError не
напоминать).

Сорри, просто забыл выкинуть имя пакета из реального кода в ходе упрощения
примера :)


--
Vadim

Victor Krapivin

unread,
Mar 13, 2008, 8:09:01 AM3/13/08
to
> Так вот - логгинг DLL показывает что DllMain вызывается огромную кучу раз. Я в DLL не
> силён, но я так понимаю, что DllMain дёргается именно при первоначальной
> загрузке DLL.

Тредов поменьше лепи.

BOOL WINAPI DllMain(
HANDLE hinstDLL,
DWORD dwReason,
LPVOID lpvReserved
);

dwReason

[in] Specifies a flag indicating why the DLL entry-point function is
being called. This parameter can be set to one of the following values.

DLL_PROCESS_ATTACH Indicates that the DLL is being loaded into the
virtual address space of the current process as a result of the process
starting up or as a result of a call to LoadLibrary. DLLs can use this
opportunity to initialize any instance data or to use the TlsAlloc
function to allocate a thread local storage (TLS) index.

DLL_THREAD_ATTACH Indicates that the current process is creating a new
thread. When this occurs, the system calls the entry-point function of
all DLLs currently attached to the process. The call is made in the
context of the new thread. DLLs can use this opportunity to initialize a
TLS slot for the thread. A thread calling the DLL entry-point function
with DLL_PROCESS_ATTACH does not call the DLL entry-point function with
DLL_THREAD_ATTACH.

Note The entry-point of a DLL function is called with this value only
by threads created after the DLL is loaded by the process. When a DLL is
loaded using LoadLibrary, existing threads do not call the entry-point
function of the newly loaded DLL.

DLL_THREAD_DETACH Indicates that a thread is exiting cleanly. If the DLL
has stored a pointer to allocated memory in a TLS slot, it uses this
opportunity to free the memory. The system calls the entry-point
function of all currently loaded DLLs with this value. The call is made
in the context of the exiting thread.

DLL_PROCESS_DETACH Indicates that the DLL is being unloaded from the
virtual address space of the calling process as a result of either a
process exit or a call to FreeLibrary. The DLL can use this opportunity
to call the TlsFree function to free any TLS indexes allocated by using
TlsAlloc and to free any thread local data.

--
Viktor

Vadim Markin

unread,
Mar 13, 2008, 10:32:15 AM3/13/08
to
Victor,

Прочёл твой ответ в google groups :)

Hе совсем ясно кто определяет это поведение dll. DllMain дёргается неявно
где-то из JNI. Как мне повлиять на это из Java?


--
Vadim

Victor Krapivin

unread,
Mar 13, 2008, 3:15:04 PM3/13/08
to
> Hе совсем ясно кто определяет это поведение dll. DllMain дёргается неявно где-то из JNI.
> Как мне повлиять на это из Java?

Когда ты создаешь новую нитку - об этом оповещаются все подтянутые к ней
DLL. И когда уничтожаешь. Hе создавай их пачками. заведи пул ниток и
раздавай им работу. И никаких дерганий.

--
Viktor

0 new messages