【问题标题】:Access Violation Error only when running user service account仅在运行用户服务帐户时出现访问冲突错误
【发布时间】:2020-12-16 10:18:20
【问题描述】:

在我们现有的应用程序中,我们使用具有 DLL 导入的 Windows 服务来调用本机程序集中的某些函数。只要调用此本机函数的服务作为网络服务运行,它就可以正常工作。当服务作为 LocalSystem 运行时,它也可以工作。

为了设置自定义权限,我们决定添加对使用服务帐户的支持。但是,简单地将服务作为新创建的 windows 用户帐户运行会导致 Native 方法调用期间出现访问冲突错误。

同样,我尝试使用对系统具有管理员权限的域帐户来运行该服务,但仍然导致同样的问题。

我在 MSDN 上找不到任何文档,其中列出了用户帐户的特定权限以使 DLL 导入工作。谁能告诉我我可能做错了什么?

C#代码

[DllImport("lmgr11.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public static extern string GetFeaturelist(short jobType, int flag);

C 代码

char* WINAPI GetFeaturelist(int type, int flag)
{
    char * featureList;
    char ** features, ** tempFeatureList;
    int counter = 0;

    if (!lm_job)
    {   
        return NULL;
    }

    features = tempFeatureList = lc_feat_list(lm_job,flag,NULL);

    //loop to know the number of features
    while(*tempFeatureList != NULL)
    { 
        counter++;
        tempFeatureList++;
    }

    featureList =  (char*) CoTaskMemAlloc(counter * 50);
    strcpy(featureList,"");

    while(*features != NULL)
    {
        strcat(featureList,*features);   
        strcat(featureList,"$$&&#&&$$");
        features++;
    }
    lc_log(lm_job, featureList);
    return featureList; 
}

【问题讨论】:

  • 做一些调试。据我们所知,异常发生在该 C 代码中。毕竟,硬编码的 50 似乎是一种风险。那里的一些错误检查看起来很可疑。 Stack Overflow 不能替代调试。也许您还没有进行任何调试,因为代码在您无法将 VS 调试器附加到的服务上下文中运行。美好的。使用跟踪调试。
  • DLL 将与用户环境(包括凭据)一起运行。您需要在服务器上正确设置角色。请参阅:docs.microsoft.com/en-us/aspnet/web-forms/overview/…

标签: c# c dllimport


【解决方案1】:

问题是C代码中的“featurelist”变量为空,下面的代码不足以处理它。

while(*features != NULL)

使用下面的代码添加另一个检查,避免崩溃。

features != NULL

至于仅在不作为“网络服务”运行时发生的错误,第 3 方代码 (lc_feat_list) 依赖于 HKU 中用户特定的注册表项,而文档错误地引用了 HKLM 下的静态注册表项,最终导致它返回一个空值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多