注册 登录
编程论坛 PowerBuilder

怪:VC编的DLL,在W7,W2003中可以被PB调用,但在W2008R2中就不行,求高手解惑

SunYX 发布于 2013-05-14 11:29, 2418 次点击
// extast.cpp : 定义 DLL 的初始化例程。
//

#include "stdafx.h"
#include "zrapi.h"
#include "Wtsapi32.h"

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define WORKING_BUFFER_SIZE 15000
#define MAX_TRIES 3

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

//
//TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
//        则从此 DLL 导出的任何调入
//        MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
//        该函数的最前面。
//
//        例如:
//
//        extern "C" BOOL PASCAL EXPORT ExportedFunction()
//        {
//            AFX_MANAGE_STATE(AfxGetStaticModuleState());
//            // 此处为普通函数体
//        }
//
//        此宏先于任何 MFC 调用
//        出现在每个函数中十分重要。这意味着
//        它必须作为函数中的第一个语句
//        出现,甚至先于所有对象变量声明,
//        这是因为它们的构造函数可能生成 MFC
//        DLL 调用。
//
//        有关其他详细信息,
//        请参阅 MFC 技术说明 33 和 58。
//

// CextastApp

BEGIN_MESSAGE_MAP(CzrapiApp, CWinApp)
END_MESSAGE_MAP()


// CextastApp 构造

CzrapiApp::CzrapiApp()
{
    // TODO: 在此处添加构造代码,
    // 将所有重要的初始化放置在 InitInstance 中
}


// 唯一的一个 CextastApp 对象

CzrapiApp theApp;


// CextastApp 初始化

BOOL CzrapiApp::InitInstance()
{
    CWinApp::InitInstance();
    return TRUE;
}







/*
测试用
*/
extern "C" _declspec (dllexport) int __stdcall zr_Test (LPTSTR ret)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    //----------------------------

    GetWindowsDirectoryA (ret, 256);
    return true;
};






extern "C" __declspec (dllexport) int __stdcall zr_GetAdaptersInfo (LPTSTR ret) {
    PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    DWORD dwRetVal = 0, dwReturn = 1;
    ULONG ulBufLen, ulOutBufLen;
    UINT i;

    ulBufLen = strlen (ret);
    *ret='\0';

    ulOutBufLen = sizeof (IP_ADAPTER_INFO);
    pAdapterInfo = (PIP_ADAPTER_INFO)  MALLOC (ulOutBufLen);
    if (pAdapterInfo == NULL) {
        return -1;    //printf("Error allocating memory needed to call GetAdaptersinfo\n");
    }

    // 第一次调用GetAdapterInfo确定ulOutBufLen大小
    if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
        FREE (pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC (ulOutBufLen);
        if (pAdapterInfo == NULL) {
            return -2;    // printf("Error reallocating memory needed to call GetAdaptersinfo\n");
        }
    }

    if (ulOutBufLen > ulBufLen) {
        return -3;    // 传入的参数不够长
    }

    dwRetVal = GetAdaptersInfo (pAdapterInfo, &ulOutBufLen);
    if (dwRetVal == NO_ERROR) {
        IP_ADDR_STRING * pIPAddr;

        pAdapter = pAdapterInfo;
        while (pAdapter) {
            /* returning salmple :
                AdapterName:{5C68F1C7-E2C3-4FFC-8ABE-18A70DDBB792}
                Description:Realtek RTL8139 Family PCI Fast Ethernet NIC
                Address:00-14-78-0d-d2-ad
                Index:65539
                Type:Ethernet
                IpAddressList:192.168.1.111/255.255.255.0
                GatewayList:
                -*-*-*-*-*-*
                AdapterName:{B445B98F-72D6-4125-B918-86CAFF932CBB}
                Description:Realtek PCIe GBE Family Controller
                Address:bc-ae-c5-b6-c7-2b
                Index:65540
                Type:Ethernet
                IpAddressList:192.168.0.136/255.255.255.0    192.168.0.111/255.255.255.0
                GatewayList:192.168.0.253    192.168.0.1    192.168.0.2
                -*-*-*-*-*-*
            */
            // AdapterName
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sAdapterName:%s\n" ,ret ,pAdapter->AdapterName);

            // Description
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sDescription:%s\n" ,ret ,pAdapter->Description);

            // Address MAC
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sAddress:" ,ret);
            for (i = 0; i < pAdapter->AddressLength; i++) {
                if (i > 0) {
                    sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s-" ,ret);
                }
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s%02x" ,ret ,(int) pAdapter->Address[i]);
                /*
                if (i == (pAdapter->AddressLength - 1))
                    printf("%.2X\n", (int) pAdapter->Address[i]);
                else
                    printf("%.2X-", (int) pAdapter->Address[i]);
                */
            }
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s\n" ,ret);

            // Index
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sIndex:%d\n" ,ret ,pAdapter->Index);

            // Type
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sType:" ,ret);
            switch (pAdapter->Type) {
            case MIB_IF_TYPE_OTHER:
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sOther" ,ret);
                break;
            case MIB_IF_TYPE_ETHERNET:
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sEthernet" ,ret);
                break;
            case MIB_IF_TYPE_TOKENRING:
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sTokenRing" ,ret);
                break;
            case MIB_IF_TYPE_FDDI:
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sFDDI" ,ret);
                break;
            case MIB_IF_TYPE_PPP:
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sPPP" ,ret);
                break;
            case MIB_IF_TYPE_LOOPBACK:
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sLookback" ,ret);
                break;
            case MIB_IF_TYPE_SLIP:
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sSlip" ,ret);
                break;
            default:
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sUnknown" ,ret);
                break;
            }
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s\n" ,ret);

            // IpAddressList
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sIpAddressList:" ,ret);
            i = 0;
            pIPAddr = &(pAdapter->IpAddressList);
            while (pIPAddr) {
                if (i > 0) {
                    sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s\t" ,ret);
                }
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s%s/%s" ,ret ,pIPAddr->IpAddress.String ,pIPAddr->IpMask.String);
                pIPAddr = pIPAddr->Next;
                i ++;
            }
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s\n" ,ret);

            // GatewayList
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%sGatewayList:" ,ret);
            i = 0;
            pIPAddr = &(pAdapter->GatewayList);
            while (pIPAddr) {
                if (i > 0) {
                    sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s\t" ,ret);
                }
                sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s%s" ,ret ,pIPAddr->IpAddress.String);
                pIPAddr = pIPAddr->Next;
                i ++;
            }
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s\n" ,ret);

            // The end
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s-*-*-*-*-*-*\n" ,ret);

            /*
            sprintf_s (ret ,ulBufLen - strlen (ret) ,"%s%s/%s/%s/%s/%s/%02x-%02x-%02x-%02x-%02x-%02x\n"
                    ,ret
                    ,pAdapter->AdapterName
                    ,pAdapter->Description
                    ,pAdapter->IpAddressList.IpAddress.String
                    ,pAdapter->IpAddressList.IpMask.String
                    ,pAdapter->GatewayList.IpAddress.String
                    ,pAdapter->Address[0]
                    ,pAdapter->Address[1]
                    ,pAdapter->Address[2]
                    ,pAdapter->Address[3]
                    ,pAdapter->Address[4]
                    ,pAdapter->Address[5]
                    );
            */
            pAdapter = pAdapter->Next;
        }// end while
    } else {
        if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
            dwReturn = -11;
        } else if (dwRetVal == ERROR_INVALID_DATA) {
            dwReturn = -12;
        } else if (dwRetVal == ERROR_INVALID_PARAMETER) {
            dwReturn = -13;
        } else if (dwRetVal == ERROR_NO_DATA) {
            dwReturn = -14;
        } else if (dwRetVal == ERROR_NOT_SUPPORTED) {
            dwReturn = -15;
        } else {
            dwReturn = -20;
        }
    }
    if (pAdapterInfo)
        FREE (pAdapterInfo);
    return dwReturn;
}
0 回复
1