在C#中调用C函数 [英] calling C function in C#

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

问题描述

我需要从c dll调用一个函数,该函数采用Uchar并返回Uchar
下面是代码,它没有给出错误,但是没有输出

Hi I need to call a function from a c dll which take a Uchar and return a Uchar
Below is the code its not giving the error but the out is not coming

#region Delegates to Exported Functions
       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
       private delegate int  DLL_U_SHAPEARABIC([In, MarshalAs(UnmanagedType.LPArray)]Char[] source, [In, Out]Int32 sourceLength, [ Out, MarshalAs(UnmanagedType.LPArray)]Char[] dest, [In, Out]Int32 destCapacity, [In, Out] Int32 options, [In, Out] ref UErrorCode pErrorCode);
       #endreg



 Char[] source = new Char[MAX_ARABIC_STR_SIZE];
            Char[] dest = new Char[MAX_ARABIC_STR_SIZE];
            

            Int32 length;
            UErrorCode errorCode = 0;
            string strDllToLoad = Application.StartupPath + @"\icuuc44.dll";
            IntPtr pLinoDll = IntPtr.Zero;

            pLinoDll = LoadLibrary(strDllToLoad);

            IntPtr u_shape_arabic = IntPtr.Zero;

            u_shape_arabic = GetProcAddress(pLinoDll, "u_shapeArabic_44");

            DLL_U_SHAPEARABIC dll_u_shapeArabic = (DLL_U_SHAPEARABIC)Marshal.GetDelegateForFunctionPointer(u_shape_arabic, typeof(DLL_U_SHAPEARABIC));
            length = strInput.Length;
            length = dll_u_shapeArabic(source, MAX_ARABIC_STR_SIZE - 1,
                     dest, MAX_ARABIC_STR_SIZE,
                     U_SHAPE_LETTERS_SHAPE | // required 
                     U_SHAPE_PRESERVE_PRESENTATION | // helpful
                     U_SHAPE_TASHKEEL_RESIZE | // to remove extra spaces
                     SHAPE_TAIL_NEW_UNICODE | // always use new codepoint
                //U_SHAPE_TEXT_DIRECTION_VISUAL_LTR | not to be used
                //U_SHAPE_AGGREGATE_TASHKEEL | // may be required, check for tashkeel
                     U_SHAPE_LENGTH_GROW_SHRINK | // default opt
                     U_SHAPE_TEXT_DIRECTION_VISUAL_RTL | // default opt
                     U_SHAPE_LAMALEF_RESIZE, // default opt
                     ref errorCode);

推荐答案

或者,我明白了...为什么要使用它?

Or, I see... Why using this?

#region Delegates to Exported Functions
//...
#endregion


我的意思是,为什么要在该区域内编写委托定义???它什么也不会做.请参见下面.

顺便说一句,无效的关键字关键字#endreg表示您提供的文本不是真实的代码,只是一些无法编译的文本...如果您没有努力正确地表达您的问题,那么您如何希望其他人会用他们宝贵的空闲时间来帮助您吗?..

无论如何,这就是我所怀疑的:查看您的委托子句,我认为您根本无法链接任何DLL导出的代码.首先,您不需要此委托.您需要带有DLLImport属性的C#声明(请参见 System.Runtime.InteropServices.DllImportAttribute ).
至于您的代表,您不需要任何东西.这只是一个类型声明,没有引用任何实际代码.

转储非托管DLL,检查函数的确切名称,使用DLLImport属性的显式DllName参数,检查所有调用约定.
如果不起作用,请正确显示您的代码,C导出文件和功能配置文件 并在此处报告-可以.


I mean, why writing the delegate definition inside this region??? It won''t do anything. Please see below.

By the way, invalid keyword keyword #endreg shows the text you provide is not a real code, just some text which will not compile... If you did not take an effort to present your problem correctly, how can you hope others will use their valuable free time to help you?..

Anyway, here is what I suspect: looking at your delegate clause I think you failed to link any DLL exported code at all. First, you do not need this delegate. You need C# declaration with DLLImport attribute (see System.Runtime.InteropServices.DllImportAttribute).
As to your delegate, you don''t need any. This is merely a type declaration with no reference to any real code.

Dump your unmanaged DLL, check up exact name of the function, use explicit DllName parameter of the DLLImport attribute, check up all calling conventions.
If it does not work, present your code, C export file and function profile accurately and report here -- will work.


这篇关于在C#中调用C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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