1

我正在使用一个老式的 ASMX WebService(不是 WCF),它在内部调用一个非托管的 DLL(通过 PInvoke)。我没有得到任何异常,但它没有按预期工作。常规 WinForms 应用程序中的相同代码可以正常工作。

我认为这是因为在 VisualStudio 中调试 WebService 时的工作目录。工作目录是:C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0,我猜非托管 DLL 丢失了。但是在我项目的 \bin 文件夹中,所有的 DLL 都在那里。

有没有办法告诉相应地配置服务还是我需要特殊的访问权限?

PS:抱歉新手问题。

这是代码的一部分:

首先:该服务接受一些矢量图形,然后由激光投影仪投影。

[WebService(Namespace = "http://www.myDomain.eu/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class BeamerService : System.Web.Services.WebService
{
    [WebMethod]
    public void ShowBounds()
    {
        // ToDo: construct some graphics

        // ToDo: get laser/DAC
        DAC dac;
        if (!Laser.DAC.TryInitializeAny(out dac))
        {
            // DAC not connected
            return;
        }

        // ToDo: send graphics to DAC

第二:访问通过USB连接到计算机的激光设备。

    public static bool TryInitializeAny(out DAC dac)
    {
        return UniversalDAC.TryInitialize(ControllerTypes.Netlase, ControllerType.Netlase, out dac);
    }

第三:使用非托管DLL(不是我的)搜索连接的设备并获取第一个。所有以“DrLava”开头的行都是对非托管代码的 P.Invoke 调用。

    public static bool TryInitialize(ControllerTypes types, ControllerType type, out DAC dac, DeviceChooser deviceChooser = null)
    {
        try
        {
            uint deviceCount = 0;
            if (DrLava.LDL_Init((uint)ConvertToNativeMask(types), ref deviceCount) != NativeConstants.DAC_OK)
                throw new ArgumentException(string.Format("Could not intialize DAC with '{0}'", type));

            uint device = deviceChooser == null ? 0 : deviceChooser(deviceCount);

            uint tType = 0;
            uint tEnum = 0;
            var sName = new StringBuilder(128);
            if (DrLava.LDL_GetDACInfo(device, sName, (uint)sName.Length, ref tType, ref tEnum) != NativeConstants.DAC_OK)
                throw new ArgumentException(string.Format("Could retriev name of DAC device number {0} with '{1}'.", device, type));

            if (DrLava.LDL_DAC_Init(device) != NativeConstants.DAC_OK)
                throw new ArgumentException(string.Format("Could not intialize DAC with '{0}' using device number {1}", type, device));

            dac = new UniversalDAC(type, sName.ToString(), device);

            return true;
        }
        catch (Exception ex)
        {
            Trace.Error("Could not initialize DAC.Instance with '{0}'. {1}", type, ex);
        }

        dac = null;
        return false;
    }

所以,问题出在第 3 步。如果我在控制台或 WinForms 应用程序中运行代码,就会找到连接的 USB 并且我可以使用它。如果我在 WebService 中运行相同的代码,步骤 3 中的第一行 (DrLava.LDL_Init) 将不会产生任何结果。

4

0 回答 0