我已经和 Canon EDSDK 斗争了一段时间了。我可以成功地让库将文件直接保存到磁盘,但是,我无法在内存中获取图像字节[]。每当我尝试将 EDSDK Stream Marshal.Copy() 到 byte[] 时,我总是会收到以下错误:
AccessViolationException:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
下面是我用来尝试获取流的代码变体之一:
private uint downloadImage(IntPtr directoryItem)
{
uint err = EDSDK.EDS_ERR_OK;
IntPtr stream = IntPtr.Zero;
// Get information of the directory item.
EDSDK.EdsDirectoryItemInfo dirItemInfo;
err = EDSDK.EdsGetDirectoryItemInfo(directoryItem, out dirItemInfo);
// Create a file stream for receiving image.
if (err == EDSDK.EDS_ERR_OK)
{
err = EDSDK.EdsCreateMemoryStream(dirItemInfo.Size, out stream);
}
// Fill the stream with the resulting image
if (err == EDSDK.EDS_ERR_OK)
{
err = EDSDK.EdsDownload(directoryItem, dirItemInfo.Size, stream);
}
// Copy the …Run Code Online (Sandbox Code Playgroud) 我的应用程序包含带有非托管C dll调用的C#代码.在我的C#代码中,我有一个对象/类,其属性是系统类型,如string和int以及我定义的其他对象.
我想将这个复杂的(Graph.cs)对象传递给我的C(dll)代码,你会在这里建议什么实现?
我已经尝试过移动结构但是我没有使用除了string和int之外的任何东西.
谢谢.
码:
public Class Grpah {
TupleCollection m_TupleCollection;
int m_nGeneralIndex;
bool m_bPrintWRF;
string m_sLink;
}
public Class TupleCollection {
IList<Tuple> _collection;
}
public Class Tuple {
Globals.TupleType m_ToppleType;
ArrayList m_Parameters;
}
public class TupleArgs {
public string Value { get; set; }
public Globals.PAS PAS;
public RefCollection RefCollection { get; set; }
public int Time{ get; set; }
}
public class RefCollection {
public List<int> SynSet{ get; set; }
public Globals.PAS PAS;
}
Run Code Online (Sandbox Code Playgroud) 有谁知道我将如何使用非托管 C ++ 等效于以下C#代码,即通过指纹从X509证书存储区中查询证书?
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var allCerts = store.Certificates;
foreach (var certificate in from X509Certificate2 certificate in allCerts
where certificate.Thumbprint != null
&& certificate.Thumbprint.Equals(thumbprint, StringComparison.OrdinalIgnoreCase)
select certificate)
{
return certificate;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
戴夫
我有一个本机/非托管DLL,它有一个"CreateObject"函数,它返回一个指向业务对象的指针..所以调用将是...... 喜欢:
[DllImport("MyDll.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateObject();
Run Code Online (Sandbox Code Playgroud)
问题:对象是暴露我想要调用的"Connect()"之类的公共函数,但我不知道如何"映射"这些调用,所以我有一个简单的方法签名,如:
private bool Connect();
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
当我看到这篇博客文章时,我正在使用XML来编写和反序列化类对象,该博客文章介绍了如何使用隔离存储区在Windows Phone 7上执行此操作.Windows Phone 7是我正在开发的平台:
在此示例中,他显式调用Dispose()的唯一对象是TextReader对象.我在MSDN上查找了TextReader对象,发现文档说这个:
释放TextReader使用的非托管资源,并可以选择释放托管资源.
所以我假设他这样做的原因是立即释放TextReader对象获取的非托管资源.如果不是因为他的博客文章,我不会想到这样做.显然我不想开始在视线中的每个对象上调用Dispose(),所以至少在调查特定对象何时应该调用Dispose()时,有什么好的经验法则?是否有一些指南或某个列表,至少是需要这种特殊处理的流行.NET对象?
我有一个C#应用程序使用一些非托管的GDI +功能,我得到一个(非托管?)异常:
Log Name: Application
Source: .NET Runtime
Date: 7/1/2013 9:14:58 AM
Event ID: 1026
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Computer: sth
Description:
Application: sth
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪是:
at System.Drawing.SafeNativeMethods+Gdip.IntGdipDisposeImage(System.Runtime.InteropServices.HandleRef)
at System.Drawing.SafeNativeMethods+Gdip.IntGdipDisposeImage(System.Runtime.InteropServices.HandleRef)
at System.Drawing.Image.Dispose(Boolean)
at System.Drawing.Image.Dispose()
at “..()
at Aspose.Cells.Charts.Chart.ToImage(System.IO.Stream, Aspose.Cells.Rendering.ImageOrPrintOptions)
Run Code Online (Sandbox Code Playgroud)
我可以防止它完全崩溃吗?我尝试过使用不带参数的catch语句,但是告诉我这还不够.我用这些有缺陷的代码创建了我自己的C++ DLL,似乎没有任何工作,除非DLL抛出:
__declspec(dllexport) void __cdecl Function1(void) // This doesn't get caught.
{
char *chrs = new char[1000];
delete []chrs;
delete []chrs; …Run Code Online (Sandbox Code Playgroud) 我已经浏览了这个网站很长一段时间并感激你对其他人的问题的答案 - 现在,唉,我必须通过问自己的一个来揭示我的无知.我搜索了一个现有的等价物,却找不到一个; 如果这是重复,我道歉.
我正在尝试使用.Net 4.0框架下的Visual C#Winforms应用程序中的非托管Windows DLL(Intel Power Gadget 3.0 API,FWIW).API是用C++实现编写的,所以我不得不翻译.我已经能够包装和实现库的大部分功能,但是我对此感到困惑:
bool GetPowerData(int iNode, int iMSR, double *pResult, int *nResult);
Run Code Online (Sandbox Code Playgroud)
......这是作者所描述的(我的重点):
返回最近一次调用ReadSample()收集的数据.返回的数据用于iNode指定的包中的数据,来自iMSR指定的MSR.数据在pResult中返回,并且在nResult中返回pResult中返回的double结果的数量.参见表1:MSR功能.
(https://software.intel.com/zh-cn/blogs/2014/01/07/using-the-intel-power-gadget-30-api-on-windows)
关键点:函数以指向双精度数组的指针的形式返回其主数据 - 或者它是指向双精度的指针数组?......什么级别的间接?这就是我开始变得不确定的地方!:)
至少有一些输入参数很简单; iNode和iMSR是ints,我知道并且已经可以获得它的值.
我已使用此签名在我的C#代码中声明了该函数:
[DllImport("EnergyLib32.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool GetPowerData(int iNode, int iMSR, out IntPtr pResult, ref int nResult);
Run Code Online (Sandbox Code Playgroud)
(我确定这是我的第一个错误,就在那里.)
至于实际调用函数,我尝试了各种各样的可能性,甚至在这里显示都太难看了.
有可能比我的知识更多的人请建议处理此功能及其返回值的最佳方法吗?
先感谢您.
我试图理解 C#/C++ 互操作的这种“它只是工作”的魔力,但目前它只是一个噩梦。
我正在玩 Mandelbrot 计算,并希望将计算核心卸载到本机 C++ 和 SSE2。这是工作,与 P/Invoke。现在我想更改为 IJW,以便更安全,因为我想了解它。但我接触 C++ 的表面已经是几十年前的事了。
我有一个struct Complex { double real; double imag; }保存 Mandelbrot 循环的起始值,我想调用这样的函数:
Compute(int vectorSize, Complex[] points, double maxValue, int maxLoops, int[] result)
现在我使用 VS Express 2013 创建了一个 CLR 类库并将其放入头文件中:
public value struct Complex
{
double real;
double imag;
};
public ref class Computations
{
public:
static void Basic(int vectorSize, array<Complex,1>^ points, double maxRadius, int maxLoops, array<int,1>^ result);
};
class NativeComputations
{
public:
static void Basic(int vectorSize, Complex* …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PInvoke来调用非托管C++ DLL,我遇到了错误(见下文).使用depends.exe我可以在导出的函数列表中看到错位的名称,因此是奇怪的EntryPoint名称.虽然出现此异常,但如果我继续进行Step Over调试,则函数返回并ptr等于1并打印"成功".
我已经尝试了其他帖子中的一些建议,但没有任何效果.我认为这uint32_t是非常自我解释的.我已经尝试更改要使用的C#PInvoke签名long,ulong但是仍然抛出异常并且值ptr非常大.我也试过了属性的设置CharSet和ExactSpelling属性,DllImport但这似乎也没有用.
我的问题是,我在做什么导致异常,如果我不能/不应该忽略异常 - 如何做到这一点?
MyClass.h
class __declspec(dllexport) MyClass
{
public:
uint32_t runCommand(uint32_t start);
};
Run Code Online (Sandbox Code Playgroud)
MyClass.cpp
uint32_t MyClass::runCommand(uint32_t start);
{
uint32_t status = 1;
return status;
}
Run Code Online (Sandbox Code Playgroud)
P/Invoke签名
[DllImport("myClass.dll",
EntryPoint = "?runCommand@MyClass@myClass@@QAEII@Z",
CallingConvention = CallingConvention.Cdecl)]
public static extern UInt32 runCommand(UInt32 baseAddress);
Run Code Online (Sandbox Code Playgroud)
用法
public static void Do()
{
UInt32 a = 0xA000;
UInt32 ptr = …Run Code Online (Sandbox Code Playgroud) 一般来说,我需要能够从我在编译时不知道的任何 DLL 中调用任何非托管函数。
我看到的所有文章(例如https://blogs.msdn.microsoft.com/jonathanswift/2006/10/03/dynamically-calling-an-unmanaged-dll-from-net-c/)都建议使用委托,但我在编译时不知道我要调用哪个函数,甚至不知道它需要哪些参数和多少参数。
基本上我有一个用户输入,如:调用“Kernel32.dll”函数“DeleteFile”参数[“C:\testfile.txt”]。
你能建议至少如何谷歌吗?“动态”这个词没有帮助..
任务本身有点疯狂,因为它实际上是一个大学项目。不确定它在现实生活中是否有用..
var dll = "kernel32.dll";
var fun = "DeleteFile";
var args = new object[] { "C:\\dev\\test.txt" };
IntPtr pDll = NativeMethods.LoadLibrary(dll);
IntPtr pFun = NativeMethods.GetProcAddress(pDll, fun);
// How can I call it in a different way, without having a delegate?
Marshal.GetDelegateForFunctionPointer(pFun, typeof(?????));
Run Code Online (Sandbox Code Playgroud)