【问题标题】:DLL Pinvoke with Windows Mobile C++ libraries porting to C#DLL Pinvoke 与 Windows Mobile C++ 库移植到 C#
【发布时间】:2012-01-22 21:13:06
【问题描述】:

我正在尝试将 OpenCV 用作here,但我在获得 PInvoke 的那一刻卡住了,即:

我的DLL.cpp

#define DLL_API __declspec(dllexport)
//...
DLL_API short processImage(const char* in_file, const char * out_file)
//...

我的form.cs

[DllImport("DLL", EntryPoint = "processImage")]
        private static extern short _ProcessImage(byte[] in_file, byte[] out_file);

        public static short binarizeImage(string in_file, string out_file)
        {
            return _ProcessImage(StringToASCIIByteArray(in_file), StringToASCIIByteArray(out_file));
        }

    public static byte[] StringToASCIIByteArray(string str)
    {
        return Encoding.ASCII.GetBytes(str + "\0");
    }

我认为这可能是目标架构的问题(在我的 VS 2008 项目中)。当我使用“任何 CPU”时,它会编译并运行但会抛出 Pinvoke,当我将其设置为“Windows Mobile 6 Professional SDK (ARMV4I)”时,它会编译但不想部署,我在输出窗口中得到了这个:

1>------ Deploy started: Project: DLL, Configuration: Debug Windows Mobile 6 Professional SDK (ARMV4I) ------
1>The system cannot find the path specified.
1>
2>------ Deploy started: Project: smartDeviceOcr, Configuration: Debug Any CPU ------
2>Deploying 'D:\VS 2008 Projects\C++\SmartDevice\ocr\smartDeviceOcr\bin\Debug\smartDeviceOcr.exe'
========== Deploy: 1 succeeded, 1 failed, 0 skipped ==========

具体的 ARMV4I 重要吗?我的手机上有ARM920T。我可以/应该编辑它以使其工作吗?


编辑:

只是为了清楚 Pinvoke 指向:

return _ProcessImage(StringToASCIIByteArray(in_file), StringToASCIIByteArray(out_file));

异常信息是:

System.MissingMethodException was unhandled
  Message="Cannot find the library DLL PInvoke 'DLL'."
  StackTrace:
    in smartDeviceOcr.Form1.binarizeImage(String in_file, String out_file)
    in smartDeviceOcr.Form1.button1_Click(Object sender, EventArgs e)
    in System.Windows.Forms.Control.OnClick(EventArgs e)
    in System.Windows.Forms.Button.OnClick(EventArgs e)
    in System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
    in System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
    in Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
    in System.Windows.Forms.Application.Run(Form fm)
    in smartDeviceOcr.Program.Main()

EDIT2:

好吧,现在真的很奇怪。我改变了一些事情。我已将 openCV 的 dll 添加到项目属性中的部署列表中,以便我可以看到它们在部署时被复制,并且我已手动将所有 dll 复制到 PC 上的 exe 目录。

我还更改了 DLL 项目中的一些部署选项,以便将 dll 复制到手机上的正确目录(自动)和 ....

现在我在运行时出现错误(尝试从 dll - opencv 访问函数时):

与设备的远程连接已丢失

【问题讨论】:

  • OpenCV 是本机代码。您需要一个针对您的处理器而构建的版本。
  • 这个版本是为 Windows 移动处理器构建的(我使用的是我在开头提到的网站上的版本。
  • pinvoke marshaller 似乎不同意。查看实际的异常消息将帮助我们帮助您诊断问题。
  • 我已将异常文本添加到答案中,但我认为它没有那么有用。
  • 这是一个简单的“找不到文件”错误。将“dll.dll”复制到与托管 exe 相同的目录。还要在那里复制opencv dll。顺便选一个更好的名字。

标签: dll opencv windows-mobile pinvoke


【解决方案1】:

由于我没有看到任何答案,所以我发布了我的答案(尽管我对此仍有疑问)

可以通过添加要部署 dll 的目录来解决问题:

Project-> Properties -> Deployment -> Remote Directory

并正确指定文件:

Project-> Properties -> Deployment -> Additional Files

highgui200.dll|$(SolutionDir)\opencv_winmo\dll|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0
cv200.dll|$(SolutionDir)\opencv_winmo\dll|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0
cxcore200.dll|$(SolutionDir)\opencv_winmo\dll|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0
ml200.dll|$(SolutionDir)\opencv_winmo\dll|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0
msvcr90.dll|$(BINDIR)\$(INSTRUCTIONSET)\|%CSIDL_PROGRAM_FILES%\smartDeviceOcr|0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-30
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多