2016-12-05 125 views
0

我正在使用CAPL執行腳本,並且堅持使用grep從Windows登錄ID的解決方案。有些人可以幫助展示如何從CAPL程序代碼中獲取Windows用戶登錄ID,如果可能的話?從CAPL訪問操作系統功能

例如,如果Windows用戶登錄ID是'kp21ml',我想從CAPL函數讀取此ID,如下所示。

byte UserIdCheck() 
{ 
    char uid[10]; 
    byte CanMessageTrasmission; 

    strncpy(uid, xxxx(), 6); // where xxxx() is the unknown OS or system function that could return the login ID ? 
    if (strncmp(uid, "kp21ml") != 0) 
    { 
    write("Access denied!"); // Message to CANoe's Write window 
    CanMessageTrasmission = 0; 
    } 
    else 
    { 
    // Access ok 
    CanMessageTrasmission = 1; 
    } 

    return CanMessageTrasmission; 
} 

我用這個CAPL本書作爲我的參考指南,這是非常好的: http://docplayer.net/15013371-Programming-with-capl.html 但我無法找到任何與系統的訪問。我很感謝你的幫助。

感謝 朱諾

回答

1

恐怕你將不能夠直接從CAPL腳本做到這一點。

我通常創建一個CAPL-DLL並將其包含在我的CANoe項目中,當我需要訪問某些操作系統級功能時。儘管我主要使用它來訪問外部設備(例如USB)或使用本地主機上的套接字與另一個程序進行交互,但其原理是相同的。

您可以通過示例在CANoe的文檔中找到更多信息,但CANoe示例中提供的CAPL-DLL源代碼有點難以理解。

我試圖去掉下面的代碼示例中的一些「不必要的」部分;這個例子將創建一個CAPL-DLL,它「公開」multiplyBy10函數,基本上允許你從你的CAPL腳本中調用multiplyBy10

#define USECDLL_FEATURE 
#define _BUILDNODELAYERDLL 

#pragma warning(disable : 4786) 

#include "cdll.h" 

#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <map> 

char  moduleName[_MAX_FNAME]; 
HINSTANCE moduleHandle; 

unsigned int 
CAPLEXPORT far CAPLPASCAL multiplyBy10 (unsigned char value) 
{ 
    unsigned int result = value * 10; 
    freopen("CONOUT$", "w", stdout); 
    std::cout << "multiplyBy10() - value: " << int(value) << ", result: " << result << std::endl; 
    return (result); 
}  

CAPL_DLL_INFO4 table[] = 
{ 
    {CDLL_VERSION_NAME, (CAPL_FARCALL)CDLL_VERSION, "", "", CAPL_DLL_CDECL, 0xABD, CDLL_EXPORT}, 
    {"multiplyBy10", (CAPL_FARCALL)multiplyBy10, "CAPL_DLL", "This is a demo function", 'L', 1, "D", "", { "value"}}, 
    {0, 0} 
}; 

CAPLEXPORT CAPL_DLL_INFO4 far * caplDllTable4 = table; 

bool 
WINAPI DllMain(HINSTANCE handle, DWORD reason, void*) 
{  
    static FILE * stream; 

    switch (reason) 
    { 
     case DLL_PROCESS_ATTACH: 
     { 
      moduleHandle = handle; 

      char path_buffer[_MAX_PATH]; 
      DWORD result = GetModuleFileName(moduleHandle, path_buffer, _MAX_PATH); 

      char drive[_MAX_DRIVE]; 
      char dir[_MAX_DIR]; 
      char fname[_MAX_FNAME]; 
      char ext[_MAX_EXT]; 

      _splitpath_s(path_buffer, drive, dir, fname, ext); 
      strcpy_s(moduleName, fname); 

      AllocConsole(); 
      freopen_s(&stream, "conout$", "w", stdout); 
      std::cout << "DLL_PROCESS_ATTACH" << std::endl; 

      return 1; 
     } 

     case DLL_PROCESS_DETACH:            
     { 
      std::cout << "DLL_PROCESS_DETACH" << std::endl; 
      FreeConsole(); 
      fclose(stream); 

      return 1; 
     } 
    } 

    return 1; 
} 
相關問題