2016-12-05 123 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; 
}