Skip to main content
 首页 » 编程设计

C#之使用固定大小的输出参数调用非托管函数

2024年02月23日16xing901022

过去 12 个小时左右,我一直在搜索,我认为此时我什至不在正确的轨道上,或者我的搜索措辞是否正确。我希望有人能在这里为我指明正确的方向。

我试图访问一个非托管库中的函数,该库将固定大小的 DWORD 数组作为其参数之一,但我不确定如何在声明或调用中指定它。我也不确定该功能的文档是否正确(或者我是否理解)。

int EloGetScreenInfo(DWORD dwMonNum[32], int iScrCnt)

Parameters: dwMonNum [in]: Array of DWORD to receive the Windows monitor number associated with the touchscreens.

iScrCnt [out]: It retrieves the total number of Elo touchscreens found.

Return Values: Returns EloSuccess if the call succeeds, it returns an error code otherwise. See Error Codes section for list of error values.

Remarks:

It returns the list of Windows monitor numbers associated with the touchscreens where, the index is the touchscreen number and the value is the Windows monitor number.

Touchscreens are 0 based and Windows monitor numbers are 1 based.

If a touchscreen is not calibrated, the windows monitor is returned as -1.

Maximum of 32 touchscreens are supported.

请注意,在描述中它声明它返回一个监视器编号列表,这表明它将是填充值,但在参数描述中它显示为 [in] 和将参数计数为 [out] 除非我误解了描述。 在 See Also Sample 链接中,它显示了一个用法示例,其中两者似乎都被填充了

    DWORD dwEnumMon[MAX_SUPPORTED_SCR] ; 
    int iScrCnt, iRet ; 
 
    ZeroMemory( dwEnumMon, MAX_SUPPORTED_SCR ) ; 
    ZeroMemory( dwMonParam, MAX_SUPPORTED_SCR ) ; 
 
    // Get the list of all Elo Serial & USB screen and monitor association 
    iRet = EloGetScreenInfo(dwEnumMon,iScrCnt) ; 
    if(iRet != EloSuccess ){ 
        printf( "Error Code = %d \n", iRet ) ; 
        return EloFailure; 
    } 
    else 
    if(iScrCnt<0){ 
        printf( "No Elo touchscreens found\n" ) ; 
        return EloFailure; 
    } 
 
    // Process Commandline 
    ProcessCmdLine( argc, argv ) ; 
 
    // For all screens of matching monitor number enable / disable touch 
    for( int i=0; i<dwMonParamCnt; i++ ){ 
        // where j is the screen number associated with the monitor number 
        for( int j=0; j<iScrCnt; j++ ){ 
            if( dwMonParam[i] == dwEnumMon[j] ){ 
                // Enables / Disables touch depending on the bFlag 
                // where j is the screen number 
                if( (iRet = EloSetTouchReportingState( bEnable , j )) == EloSuccess ) 
                    printf( "EloSetTouchReportingState Returned success\n" ); 
                else 
                    printf( "EloSetTouchReportingState Returned failed. Error \ Code=%d\n", iRet ); 
               } 
           } 
      } 
 
      return EloSuccess; 

TIA 寻求您可以提供的任何指导或帮助

请您参考如下方法:

您应该能够使用普通的 UInt32 数组来调用它。您的托管原型(prototype)将是:

[DLLImport "DllName"] 
static extern int EloGetScreenInfo(UInt32[] dwMonNum, out int iScrCnt); 

要调用它,只需指定编码:

UInt32[] MonitorNumbers = new UInt32[32]; 
int iScrCnt = 0; 
 
int rslt = EloGetScreenInfo( 
    [MarshalAs(UnmanagedType.LPArray, SizeConst=32)] MonitorNumbers, 
    out iScrCnt);