Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DLL from
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!!! Big Grin | :-D
Thanks a lot
 
From C# calling:
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:
 
extern "C"
{
  __declspec(dllexport) int DisplayHelloFromDLL()
  {
        printf ("Hello from DLL !\n");
        return 52;
  }
}
Posted 18-Jul-11 7:21am
YaMone334
Edited 18-Jul-11 7:51am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
CPallini at 18-Jul-11 13:27pm
   
Yes, it looks correct. Good advices, 5.
SAKryukov at 18-Jul-11 13:29pm
   
Thank you. Easy enough to try out and put to work. This is the simplest possible case.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
YaMone at 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)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 18 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100