警告C4312导致错误LNK2019

时间:2016-11-12 18:35:16

标签: java c++ java-native-interface 64-bit

我有一个c++文件的以下代码,用于限制JFrameJava调整大小超过一定大小。但是当我编译代码时,我得到4个关于将jint转换为更大尺寸的HWND的警告,然后我得到4个错误LNK2019: unresolved external symbol并且这些错误似乎是指使用的整数在功能; setMinMaxResizeBoundaries()getComponentHWND()

DllMain.cpp

//For JNI
#include <jni.h>
#include <jawt.h>
#include <jawt_md.h>

//Rest
#include <stdio.h>
#include <windows.h>

//My File
#include "JNITest.h"

// Global variables defined in DllMain.cpp
// Used for setMinMaxResizeBoundaries()
struct SHwndMinMax
{
    HWND    hwnd;
    int     minWidth;
    int     minHeight;
    int     maxWidth;
    int     maxHeight;
    WNDPROC prefWndProc;
};
SHwndMinMax gsHwndMinMax[2048];
int gsHwndMinMaxCount = 0;
LRESULT CALLBACK MinMaxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);


// Code added somwhere:
// setMinMaxResizeBoundaries()
// Sets the resize boundary window sizes, so the window will not be resized above/below that size
JNIEXPORT jint JNICALL Java_JNITest_setMinMaxResizeBoundaries(JNIEnv* env, jclass cls,
    jint hwnd,
    jint minWidth, jint minHeight,
    jint maxWidth, jint maxHeight)
{
    // We create a hook for the window, and intercept the WM_GETMINMAXINFO message occurs, and update the info
    if (IsWindow((HWND)hwnd))
    {   // Let's add it
        if (gsHwndMinMaxCount < 2048)
        {   // We're good
            // Can add code here to check if this option is valid or not--so it can later be "unhooked" by a separate function call
            gsHwndMinMax[gsHwndMinMaxCount].hwnd        = (HWND)hwnd;
            gsHwndMinMax[gsHwndMinMaxCount].minWidth    = minWidth;
            gsHwndMinMax[gsHwndMinMaxCount].minHeight   = minHeight;
            gsHwndMinMax[gsHwndMinMaxCount].maxWidth    = maxWidth;
            gsHwndMinMax[gsHwndMinMaxCount].maxHeight   = maxHeight;
            gsHwndMinMax[gsHwndMinMaxCount].prefWndProc = (WNDPROC)SetWindowLongPtr((HWND)hwnd, GWLP_WNDPROC, (LONG_PTR)&MinMaxWindowProc);
            // Success
            ++gsHwndMinMaxCount;
            return(0);

        } else {
            // Failuire, too many hooks
            return(-2);
        }

    } else {
        // Failure, HWND is not valid
        return(-1);
    }
}

LRESULT CALLBACK MinMaxWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int i;
    MINMAXINFO* mmi;

    for (i = 0; i < gsHwndMinMaxCount; i++)
    {
        if (hwnd == gsHwndMinMax[i].hwnd)
        {   // This is our man, see if it's our message
            if (msg == WM_GETMINMAXINFO)
            {   // It is
                // When maximized, window is at upper-left
                mmi = (MINMAXINFO*)lParam;
                mmi->ptMaxSize.x        = gsHwndMinMax[i].maxWidth;
                mmi->ptMaxSize.y        = gsHwndMinMax[i].maxHeight;
                mmi->ptMaxPosition.x    = 0;  // Can add code here to properly position the window centered in the screen, etc.
                mmi->ptMaxPosition.y    = 0;  // Same here
                // Set the minimum and maximum tracking size (when the user is resizing, what's the smallest and biggest window they see)
                mmi->ptMinTrackSize.x   = gsHwndMinMax[i].minWidth;
                mmi->ptMinTrackSize.y   = gsHwndMinMax[i].minHeight;
                mmi->ptMaxTrackSize.x   = gsHwndMinMax[i].maxWidth;
                mmi->ptMaxTrackSize.y   = gsHwndMinMax[i].maxHeight;
                return(DefWindowProc(hwnd, msg, wParam, lParam));

            } else {
                // Nope, pass it on
                return(CallWindowProc(gsHwndMinMax[i].prefWndProc, hwnd, msg, wParam, lParam));
            }
        }
    }
    return(0);
}

// getComponentHWND()
// Called to return the HWND of the component, if it has one.
JNIEXPORT jint JNICALL Java_JNITest_getComponentHWND(JNIEnv* env, jclass cls, jobject obj)
{
    HWND hWnd = 0;
    typedef jboolean (JNICALL *PJAWT_GETAWT)(JNIEnv*, JAWT*);
    JAWT awt;
    JAWT_DrawingSurface* ds;
    JAWT_DrawingSurfaceInfo* dsi;
    JAWT_Win32DrawingSurfaceInfo* dsi_win;
    jboolean result;
    jint lock;
    HMODULE _hAWT = 0;

    // Load AWT Library
    if (!_hAWT)
        _hAWT = LoadLibrary("jawt.dll");   // for Java 1.4+

    if (_hAWT)
    {
        PJAWT_GETAWT JAWT_GetAWT = (PJAWT_GETAWT)GetProcAddress(_hAWT, "JAWT_GetAWT");
        if (JAWT_GetAWT)
        {
            awt.version = JAWT_VERSION_1_4;     // Init here with JAWT_VERSION_1_3 or JAWT_VERSION_1_4
            // Get AWT API Interface
            result = JAWT_GetAWT(env, &awt);
            if (result != JNI_FALSE)
            {
                ds = awt.GetDrawingSurface(env, obj);
                if (ds != NULL)
                {
                    lock = ds->Lock(ds);
                    if ((lock & JAWT_LOCK_ERROR) == 0)
                    {
                        dsi = ds->GetDrawingSurfaceInfo(ds);
                        if (dsi)
                        {
                            dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
                            if (dsi_win)
                                hWnd = dsi_win->hwnd;
                            else
                                hWnd = (HWND) -1;   // Failed to obtain the handle (not running on Windows)

                            ds->FreeDrawingSurfaceInfo(dsi);

                        } else {
                            hWnd = (HWND)-2;    // Failed to get the drawing surface info block
                        }
                        ds->Unlock(ds);

                    } else {
                        hWnd = (HWND)-3;    // Failed to lock the drawing surface to obtain information about it
                    }
                    awt.FreeDrawingSurface(ds);

                } else {
                    hWnd = (HWND)-4;    // Failed to get the drawing surface from the compoment
                }
            } else {
                hWnd = (HWND)-5;    // Failed to obtain a proper result from _JAWT_GetAWT()
            }
        } else {
            hWnd = (HWND)-6;    // Failed to find "_JAWT_GetAWT()" function
        }
    } else {
        hWnd = (HWND)-7;    // Failed to load awt.dll
    }
    return (jint)hWnd;
}

编译器输出

DllMain.cpp
DllMain.cpp(38): warning C4312: 'type cast': conversion from 'jint' to 'HWND' of greater size
DllMain.cpp(43): warning C4312: 'type cast': conversion from 'jint' to 'HWND' of greater size
DllMain.cpp(48): warning C4312: 'type cast': conversion from 'jint' to 'HWND' of greater size
DllMain.cpp(164): warning C4311: 'type cast': pointer truncation from 'HWND' to 'jint'
Microsoft (R) Incremental Linker Version 14.00.24210.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/dll
/implib:DllMain64.lib
/out:DllMain64.dll
DllMain.obj
   Creating library DllMain64.lib and object DllMain64.exp
DllMain.obj : error LNK2019: unresolved external symbol __imp_DefWindowProcA referenced in function "__int64 __cdecl MinMaxWindowProc(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?MinMaxWindowProc@@YA_JPEAUHWND__@@I_K_J@Z)
DllMain.obj : error LNK2019: unresolved external symbol __imp_CallWindowProcA referenced in function "__int64 __cdecl MinMaxWindowProc(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?MinMaxWindowProc@@YA_JPEAUHWND__@@I_K_J@Z)
DllMain.obj : error LNK2019: unresolved external symbol __imp_IsWindow referenced in function Java_JNITest_setMinMaxResizeBoundaries
DllMain.obj : error LNK2019: unresolved external symbol __imp_SetWindowLongPtrA referenced in function Java_JNITest_setMinMaxResizeBoundaries
DllMain64.dll : fatal error LNK1120: 4 unresolved externals

感谢您解决问题的任何帮助

编译批次

call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
javac JNITest.java
javah -jni JNITest
cl -I"C:/Program Files (x86)/Java/jdk1.8.0_92/include" -I"C:/Program Files (x86)/Java/jdk1.8.0_92/include/win32" -MD -LD DllMain.cpp -FeDllMain64.dll
pause

0 个答案:

没有答案