Tag: interop

我可以选择C ++ DLL来调用C#DLL吗?

我有一个C ++ DLL需要在C#类库中调用一个函数(传递一个值,返回一个值)。 我唯一的选择是给C#DLL一个COM接口,并使用IDispatch从C ++调用它? 这是最好的方法吗?

在C ++和C#之间传递vector struct

我有c ++非托管代码,我想从c#访问。 所以我按照了一些教程,为我的项目构建了一个dll(只有一个类btw)。 现在我想从c#中使用它,我正在使用p / invoke,如下所示。 我的问题是:是否有可能对我的Windows点进行编组,以便将其作为向量传递到我的c ++代码中? 我可以更改所有代码(除了qwindows点,但我可以自己指出)。 有没有我不需要创建交流包装的解决方案? 我正在关注这个问题: 如何使用st#:: vector :: iterator作为C#中的参数调用非托管C ++函数? 非常感谢ps,我发现了一个“解决方案”,但我无法查看它http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21461195.html C# using Point = System.Windows.Point; class CPlusPlusWrapper { [DllImport(“EmotionsDLL.dll”, EntryPoint = “calibrate_to_file”)] static extern int calibrate_to_file(vector pontos);//marshall here [DllImport(“EmotionsDLL.dll”, EntryPoint = “calibration_neutral”)] static extern int calibration_neutral(); /// /// wraps c++ project into c# /// public void calibrate_to_file() { […]

Word中的子弹点与c#Interop

我有以下代码,它应该将一个项目符号列表添加到我自动生成的word文档中。 从其他答案我相信代码是正确的,但结果根本没有产生任何要点,它似乎也没有应用缩进。 有任何想法吗? Microsoft.Office.Interop.Word.Paragraph assets; assets = doc.Content.Paragraphs.Add(Type.Missing); // Some code to generate the text foreach (String asset in assetsList) { assetText = assetText + asset + “\n”; } assets.Range.ListFormat.ApplyBulletDefault(Type.Missing); // Add it to the document assets.Range.ParagraphFormat.LeftIndent = -1; assets.Range.Text = assetText; assets.Range.InsertParagraphAfter();

禁用Word 2010中的“另存为”按钮

我有以下代码应该禁用Word 2010中的另存为按钮。下面的方法是在Document_Startup事件中调用: private void DisableSaveAsButton() { Object MenuBar = 40; Object FileMenu = 1; Object SaveAsButton = 5; var saveAsBtn = this.ThisApplication.CommandBars[MenuBar].Controls[FileMenu].accChild[SaveAsButton] as CommandBarButton; saveAsBtn.Enabled = false; } 我希望Save as Button变灰,但它不是,它仍然有效。 我究竟做错了什么?

适应二维情况的包装类

这个问题是这个问题的延伸。 我想适应二维情况的包装。 这是我的第一次尝试: public class EmxArrayRealTWrapper : IDisposable { private readonly emxArray_real_T _value; private GCHandle _dataHandle; private GCHandle _sizeHandle; public emxArray_real_T Value { get { return _value; } } public EmxArrayRealTWrapper(double[,] data) { _dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned); _value.data = _dataHandle.AddrOfPinnedObject(); _sizeHandle = GCHandle.Alloc(new int[] { data.GetLength(0), data.GetLength(1) }, GCHandleType.Pinned); _value.size = _sizeHandle.AddrOfPinnedObject(); _value.allocatedSize = data.GetLength(0) […]

如何处理失败的DllImport?

我正在尝试编写一个C#托管类来包装SHGetKnownFolderPath,到目前为止它可以在Vista上运行,但由于没有像预期的那样在shell32.dll中找到正确的函数而在XP上崩溃。 我想让它设置好所以如果使用XP,我可以使用System.Environment.GetFolderPath来回避(通常是hacky)解决方案。 (或者,更好的是,如果它在shell32中找不到function。) 除了条件编译之外,还有什么方法可以做到吗? 我目前的代码如下: public abstract class KnownFolders { [DllImport(“shell32.dll”)] private static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath); // Trim properties to get various Guids. public static string GetKnownFolderPath(Guid guid) { IntPtr pPath; int result = SHGetKnownFolderPath(guid, 0, IntPtr.Zero, out pPath); if (result == 0) { string s […]

ASP.NET Web应用程序在IIS Web服务器上调用Delphi DLL,在返回PChar字符串时锁定

如果我不返回任何内容,则工作正常,或者返回一个整数。 但如果我试图返回一个PChar,即… result := PChar(”) or result:= PChar(‘Hello’) 网络应用程序只是冻结,我看到它的内存计数在任务管理器中逐渐变得越来越高。 奇怪的是,DLL在VStudio调试服务器或C#应用程序上运行良好。 我唯一能想到的就是IIS服务器在64位Windows上运行。 它似乎不是一个兼容性问题,因为我可以成功写入文本文件并从DLL中执行其他操作…我只是不能返回一个PChar字符串。 尝试使用PWideChar,尝试返回’something \ 0’,尝试了我能想到的一切。 不幸的是没有运气。 [DllImport(“TheLib.dll”, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] private static extern string SomeFunction(); string result = SomeFunction(); delphi: library TheLib; function SomeFunction() : PChar export; stdcall; begin return PChar(”); end; exports SomeFunction

使用COM互操作从BS ++到C#编组BSTR

我有一个用C ++编写的进程外COM服务器,它由一些C#客户端代码调用。 其中一个服务器接口上的方法将大型BSTR返回给客户端,我怀疑这会导致内存泄漏。 该代码有效,但我正在寻找有关编组BSTR的帮助。 简化一下,服务器方法的IDL是 HRESULT ProcessRequest([in] BSTR request, [out] BSTR* pResponse); 并且实现如下: HRESULT MyClass::ProcessRequest(BSTR request, BSTR* pResponse) { USES_CONVERSION; char* pszRequest = OLE2A(request); char* pszResponse = BuildResponse(pszRequest); delete pszRequest; *pResponse = A2BSTR(pszResponse); delete pszResponse; return S_OK; } A2BSTR在内部使用SysAllocStringLen()分配BSTR。 在C#客户端中,我只需执行以下操作: string request = “something”; string response = “”; myserver.ProcessRequest(request, out response); DoSomething(response); 这样做,因为请求字符串被发送到COM服务器并且正确的响应字符串被返回给C#客户端。 但是每次往返服务器都会泄漏服务器进程中的内存。 crt泄漏检测支持显示crt堆上没有重大泄漏,所以我怀疑泄漏是用IMalloc分配的。 […]

PCWSTR与LPWSTR

这是我的理解(如果我错了,请纠正我),它们之间的唯一区别是字符串是否可能被被调用函数修改。 ( PCWSTR , LPWSTR ) 我现在正试图将一个字符串从C#传递给一个期望PCWSTR的函数,但我能找到的只是[MarshalAs(UnmanagedType.LPWStr)] 。 我纠正那没关系吗? (是的,它有效 。但是,这并不能certificate它没问题。有些东西可以工作,但会导致内存泄漏等)

找不到方法:’System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)’

我花了很多时间在我的开发盒上编写这个程序,把它移到我们的生产盒后我得到了下面的错误。 仅仅是一个FYI我无法控制已安装的内容和可安装的内容,我该如何使其工作? 在两台计算机的两个框架下,我们都有v1.0.3705,v1.1.4322,v2.0.50727,v3.0,v3.5,4.0.30319。 我用来创建应用程序的程序也是Visual Studio 2013 Pro。 谢谢 有关调用实时(JIT)调试而不是此对话框的详细信息,请参阅此消息的结尾。 ** * ** exception文本 ** * **** System.MissingMethodException:找不到方法:’System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)’。 at C_Sharp_version.Form1.button4_Click(Object sender,EventArgs e) 在System.Windows.Forms.Forn.En上的System.Windows.Forms.Button.OnClick(EventArgs e)中的System.Windows.Forms.Control.OnClick(EventArgs e)处于System.Windows.Forms.Control的System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) System.Windows.Forms.Button.WndProc上System.Windows.Forms.ButtonBase.WndProc(Message&m)的System.Windows.Forms.Control.WndProc(Message&m)处的.WmMouseUp(Message&m,MouseButtons按钮,Int32单击) (Message&m)System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,IntPtr wparam,IntPtr lparam) ** * ** 已加载的程序集 ** * **** mscorlib程序集版本:4.0.0.0 Win32版本:4.0.30319.239(RTMGDR.030319-2300)CodeBase:file:/// C:/Windows/Microsoft.NET/ Framework / v4.0.30319 / mscorlib.dll —————————————- C夏普版程序集版本:1.0.0.0 Win32版本:1.0.0.0 CodeBase:file:/// gordonc […]