如何调用从C#C函数与WCHAR * out参数? [英] how to call a C function from C# with a WCHAR* out parameter?

查看:370
本文介绍了如何调用从C#C函数与WCHAR * out参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点问题,编组,我只是不能由我自己看着办吧。我搜索过这个话题,但还没有任何运气还没有,所以基本上我想从我的托管C#应用程序调用非托管的C函数。 C函数的签名是这样的:

I'm having a bit of problem with marshaling and I just can't figure it out by myself. I've searched for this topic, but hadn't have any luck yet, so basically I'm trying to call an unmanaged C function from my managed C# application. The signature of the C function looks like this:

long MyFunction(WCHAR* upn, long upnSize, WCHAR* guid, long* guidSize);

我不访问该.dll文件,我只知道,该功能被曝光的使用,我知道什么功能是应该做的,但我不知道里面有什么情况发生,所以函数接收一个WCHAR * UPN拿着UserPricipalName和长用附带的UPN的长度。还一个WCHAR指针传递沿,其中,函数写回其与传递UPN相关联的相应的GUID。该guidSize指针供应指针的大小,如果是太小书面GUID没有完全写入。如果一切顺利的函数应返回0(它从来没有发生过,从C#调用时)

I don't access to the .dll file, I just know that the function is being exposed for usage and I know what the function is supposed to do, but I don't know what happening inside, so the function receives a WCHAR* upn holding a UserPricipalName and a long with the length of the supplied UPN. Also a WCHAR pointer is passed along, where the function writes back a corresponding GUID which is associated with the passed UPN. The guidSize pointer supplies the size of the pointer, if it's too small the written GUID is not fully written. If everything goes fine the function should return 0 (it never happened yet, when called from c#)

现在我的努力来调用和调用这个函数是这样的:

Now my efforts to invoke and call this function look like this:

[DllImport(@"MyDll.dll", EntryPoint = "MyFunction", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern long MyFunction(IntPtr upnPtr, long upnSize, [Out, MarshalAsAttribute(UnmanagedType.LPWStr) ] StringBuilder guidPtr, IntPtr guidSizePtr);


//my attempt to call the Dll's exposed function
string upn = foo@bar.com;
long upnSize = upn.Length;
IntPtr upnPtr = Marshal.StringToHGlobalUni(upn);

IntPtr guidSizePtr = Marshal.AllocHGlobal(sizeof(long));
Marshal.WriteInt32(GuidSizePtr, 128);
var guidSB = new StringBuilder(128);

result = MyFunction(upnPtr, upnSize, guidSB, guidSizePtr);

作为一个结果,我收到一个AccessViolationException。我打得周围的许多变化来调用函数,但我从来没有成功地接收0作为返回值,我从来没有能够读出GUID作为我应该做的。

as a result I receive an AccessViolationException. I've played around with many variations to call the function, but I never managed to receive a 0 as return value and I was never able to read out the GUID as I'm supposed to do.

任何帮助,这将是AP preciated。

Any help with this would be appreciated.

推荐答案

将函数声明为:

    [DllImport(@"MyDll.dll", EntryPoint = "MyFunction", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    public static extern int MyFunction([MarshalAsAttribute(UnmanagedType.LPWStr)] string upnPtr, int upnSize, [MarshalAsAttribute(UnmanagedType.LPWStr)] StringBuilder guidPtr, ref int guidSizePtr);

调用它,如下所示:

Call it as follows:

        string upn = "foo@bar.com";
        var guidSB = new StringBuilder(128);
        int guidSizePtr =guidSB.Capacity;
        MyFunction(upn, upn.Length, guidSB, ref guidSizePtr);

注意,长期在C ++是32位的,所以你应该定义你的C#code所有这些情况下为int。

这篇关于如何调用从C#C函数与WCHAR * out参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆