【问题标题】:Access to OS functions from CAPL从 CAPL 访问操作系统功能
【发布时间】:2016-12-19 22:54:59
【问题描述】:

我正在使用 CAPL 编写脚本,但我一直在寻找从 Windows grep 登录 ID 的解决方案。如果可能的话,请帮助展示如何从 CAPL 程序代码中获取 Windows 用户登录 ID?

例如,如果 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 但是我找不到与系统访问有关的任何东西。非常感谢您的帮助。

谢谢 朱诺

【问题讨论】:

    标签: operating-system capl canoe


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 2013-06-14
      • 2012-05-20
      • 1970-01-01
      相关资源
      最近更新 更多