Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
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
C#
#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);
Posted
Updated 12-Dec-10 2:50am
v3
Comments
Henry Minute 20-Sep-10 20:01pm    
Your code snippet appears to be incomplete. Would you please check? It would probably also help if you added the code that calls your delegate.
Sergey Alexandrovich Kryukov 10-Dec-10 9:30am    
Please publish here full profile of the C function, export file (if any) and -- a separate section -- complete C# code you proposed. Is C program Unicode or not? The problem looks quite simple, but your calling convention looks a bit suspicious. Where is your DllImport attribute? Did you dump DLL, checkup name, used explicit DLLName parameter of DllImport attribute.
Abdul Quader Mamun 12-Dec-10 8:50am    
Spelling Check.

1 solution

Or, I see... Why using this?

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

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.
 
Share this answer
 
v5
Comments
Espen Harlinn 26-Feb-11 10:47am    
Good - 5
Sergey Alexandrovich Kryukov 26-Feb-11 14:35pm    
Thanks lot,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900