Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi ,
Please can anyone help me ?
i m trying to call a function say getdevice();
which is present in my unmanaged dll. the function in dll has [b]parameters say
getdevice(char **pDeviceList,int *pDeviceCount, unsigned longulFlags)
actually my dll is in c++ and whole coding of my application is also in c++ but i want to import my dll and call my function from code written in c#, I have code to import my dll but dont know how to call the function and what parametrs i have to pass from main()
i dont know the proper datatypes and proper way for the parameters to pass while calling my function present in dll from my c# code.

Is it possible to pass pointers as parametrs while calling a function present in dll from main()in c#
because as it can be seen above function(getdevice()) has poniters as parameters.
In c# Is there any way to pass pointers through a function to call a function from main()

Aasif Eqbal
aasifaslamy85@gmail.com
Posted

You may try and tweak this:
C#
[DllImport(unmanaged.dll), CharSet=CharSet.Unicode, ExactSpelling=true, PreserveSig=false)]
private static extern int getdevice(string[] pDeviceList, IntPtr pDeviceCount, ulong ulFlags)
 
Share this answer
 
v4
Another option may be this:

[DllImport(unmanaged.dll), CharSet=CharSet.Unicode, ExactSpelling=true, PreserveSig=false)]
private static extern int getdevice(ref StringBuilder pDeviceList, IntPtr pDeviceCount, ulong ulFlags);

In the example above, you would want to construct the StringBuilder with an initial capacity larger than the expected size of the return string. This can be done like the following example:

StringBuilder deviceList = new StringBuilder(256);



You are going to have to experiment with the pDeviceList parameter. The function prototype you use depends on how that parameter is used in C/C++.
 
Share this answer
 
v3
Thank you,
well please can u send me the whole code that how to call a function from an unmanged dll.
my function
EXPORT_IMPORT int __cdecl getDevices ( char ** pDeviceList, int * pDeviceCount ,unsigned long ulFlags ) this is in c++
i m working in c# and want to import dll and need to call function in my main from my unmanged dll.
please note that i have to pass parametrs while calling function in main accrding to the parameterts which i have mention in my function prototype above
so just send me the whole code to call this function.
i have code to import my dll just ahve a look on it--

[DllImport(@"C:\WINDOWS\WinSxS\unmanged.dll", SetLastError = true)]
public static unsafe extern int getDevices(char ** pDeviceList, int * pDeviceCount, uint ulFlags);

i m sucessfully able to import my dll but not able to call function i m getting error

Error 1 The best overloaded method match for 'ConsoleApplication2.getDevices() has some invalid arguments


Error 2 Argument '1': cannot convert from 'char**' to 'ref System.Text.StringBuilder'

Error 3 Argument '2': cannot convert from 'int*' to 'System.IntPtr'

when i pass these parametrts
let say getDevice(ref StringBuilder pDeviceList, IntPtr pDeviceCount, ulong ulFlags);

tell me what parametrs i have to pass from my function equivalent to this arguments while calling function in main from my unmanaged dll.
please note that i need to know what parametrs i have to pass from my function in order to call my function.
because i have several function with parametrs of different data types.
so please let me know sir abt it as soon as possible.

Aasif Eqbal Aslam
aasifaslamy85@gmail.com
 
Share this answer
 
v2

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