Thread: Using DLL

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    2

    Using DLL

    Hi,

    I'm trying to write a program in Java which detects how long a computer has been idle. This can't be accomplished in pure Java so I have to interface with some C/C++ code. I found a suitable Windows DLL at http://www.codeproject.com/dll/trackuseridle.asp.

    The header for the DLL looks like this:

    Code:
    __declspec(dllimport) BOOL IdleTrackerInit();
    __declspec(dllimport) void IdleTrackerTerm();
    __declspec(dllimport) DWORD IdleTrackerGetLastTickCount();
    I've written the following C to try and use the functionality provided by the DLL from within Java:

    Code:
    #include "IdleTracker.h"
    #include "IdleTimer.h"
    #include <jni.h>
    #include <windows.h>
    
    #define MILLISECONDS 1000
    
    JNIEXPORT jboolean JNICALL 
    Java_IdleTimer_init(JNIEnv *env, jobject obj) 
    {
      return (jboolean)IdleTrackerInit();
    }
    JNIEXPORT void JNICALL 
    Java_IdleTimer_terminate(JNIEnv *env, jobject obj) 
    {
      IdleTrackerTerm();
    }
    JNIEXPORT jint JNICALL 
    Java_IdleTimer_idleTime(JNIEnv *env, jobject obj) 
    {
      return (jint)(GetTickCount() - IdleTrackerGetLastTickCount()) / MILLISECONDS;
    }
    However, when I try to compile with GCC, I get the following errors:

    c:\windows\TEMP/cc22zcgb.o(.text+0x7):idletime.c: undefined reference to `_imp__IdleTrackerInit'
    c:\windows\TEMP/cc22zcgb.o(.text+0x1d):idletime.c: undefined reference to `_imp__IdleTrackerTerm'
    c:\windows\TEMP/cc22zcgb.o(.text+0x36):idletime.c: undefined reference to `_imp__IdleTrackerGetLastTickCount'

    I know my Java and a good deal of C but nothing as far as C++ or Windows/DLLs. I wasn't sure if trying to access a C++ DLL from C was my problem.

    Can anyone point me in the right direction to solve the problem? Any help much appreciated.

  2. #2
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by hyvas

    Code:
    #include "IdleTracker.h"
    #include "IdleTimer.h"
    #include <jni.h>
    #include <windows.h>
    I'm not sure, but unless jni.h is inside **path_to_msvc++**\Include\, shouldn't it be double quoted (#include "jni.h")?

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    AFAIK __declspec(dllimport) is a MSVC qualifier so getting it to work with GCC will be difficult before you get to the troubles of name mangling

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    2
    Thanks. I'll try giving MSVC a go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 05:24 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM