Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This project is useful for me and now I am using window form not console application form and trying to pass the int value from DLL in C to C#. But Everytime I received is 17, I don't know what happened. Please help me with this, only when i succeed this step , I can go on, so poor!!! :D
Thanks a lot

From C# calling:
C#
namespace GUI
{
    public partial class Form1 : Form
    {
        
        [DllImport("YaMone.dll")]
        public static extern int DisplayHelloFromDLL();
        private void button1_Click(object sender, EventArgs e)
        {

            label10.Text = "This is C# program";
            int fighting = DisplayHelloFromDLL();
            textBox1.Text = fighting.ToString();
        }
}


From DLL in C:

C
extern "C"
{
  __declspec(dllexport) int DisplayHelloFromDLL()
  {
        printf ("Hello from DLL !\n");
        return 52;
  }
}
Posted
Updated 18-Jul-11 6:51am
v2

It looks basically correct. Try to explicitly specify naming convention on both sides, for example __cdecl in C++ and CallingConvention equals to CallingConvention.Cdecl in System.Runtime.InteropServices.DllImportAttribute.

I would also advice to explicitly name entry point by using System.Runtime.InteropServices.DllImportAttribute.EntryPoint. In this way, you don't have to use extern "C" and bind the entry point even if the name is decorated or mangled (http://en.wikipedia.org/wiki/Name_mangling[^]). To see what the actual names are, use some binary dump utility like DUMPBIN.EXE, which is bundled with all versions of Visual Studio; you can use it from Visual Studio Command Prompt. See http://msdn.microsoft.com/en-us/library/c1h23y6c%28v=vs.71%29.aspx[^].

Also, I want to note that you use console in your sample DLL code but test it with Windows .NET application where the console is not shown by default. To show console, don't change anything in the project but application type. Set it to "Console Application" in the project Properties — your application will work as console and window application at the same time.

—SA
 
Share this answer
 
v2
Comments
CPallini 18-Jul-11 13:27pm    
Yes, it looks correct. Good advices, 5.
Sergey Alexandrovich Kryukov 18-Jul-11 13:29pm    
Thank you. Easy enough to try out and put to work. This is the simplest possible case.
--SA
Add calling convention to the dll, like:
extern "C"
{
  __declspec(dllexport) int __stdcall DisplayHelloFromDLL()
  {
        printf ("Hello from DLL !\n");
        return 52;
  }
}


Best regards
Espen Harlinn
 
Share this answer
 
Comments
YaMone 22-Jul-11 1:12am    
Thank you very much, for me , as I am not an expert in C programming, solution 1 is easier for me to implement it. For solution 1 , it will be better to write it in that way for more organizing, I don't know how to write the code for callingconvention and entry point although I tried.If you don't mind, could you please add some example code for me to learn.
Relating to the application,although the part " printf is ok to print in console application with common prompt" , how can I print that part in Windows Forms application as windows require listbox or textbox to display it?
It show error when i run this as It does not return anything.but otherwise how can I place that printf value into listbox for eg.
" listbox1.Text= DisplayHelloFromDLL().ToString();

Thank you anyway!!! :D

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