{"id":181,"date":"2009-07-24T22:41:09","date_gmt":"2009-07-24T21:41:09","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=181"},"modified":"2009-07-24T22:47:53","modified_gmt":"2009-07-24T21:47:53","slug":"c-dll-objects-accessed-from-c","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/c-dll-objects-accessed-from-c\/","title":{"rendered":"C++ DLL Objects accessed from C#"},"content":{"rendered":"<p>Because there is a few things that c# cannot do compared to c++, e.g. cpu\/memory management.  And also there is probably allot of dll&#8217;s out there are still required for some events, c# is able to communicate with these files and use there functions within the c# language.<\/p>\n<p>To create an dll within Visual Studio 2005 within the language c++, if you do the following<\/p>\n<p>1. New project -> c++ -> empty project<br \/>\n2. Enter project name (e.g. hellocdll)<br \/>\n3. Right click source files ( in the solution explorer) add-> new item<br \/>\nenter an cpp filename.<br \/>\n4, Right click on the main project heading in the solution explorer -> properties<br \/>\nconfiguration properties->general inner screen project defaults -> configuration type, alter to dynamic library (.dll)<br \/>\n5. Copy and paste code and then compile.<\/p>\n<pre lang=\"cpp\">\r\n#include <stdio.h>\r\n\r\nextern \"C\"\r\n{\r\n  __declspec(dllexport) void DisplayMessageFromDLL()\r\n  {\r\n              printf (\"Hi From the C DLL!\\n\");\r\n  }\r\n\r\n  __declspec(dllexport) int DisplayValueAndReturn(int i)\r\n  {\r\n         printf(\"Value %i\\n\", i);\r\n         return i+2;\r\n  }\r\n}\r\n<\/pre>\n<p>The extern &#8220;C&#8221; means that the references within the code are going to be available externally and marco __declsepc(dllexport) is for the MS compile to inform that functions are to be available within an dll file. <\/p>\n<p>To create an c# project to communicate with the above dll<\/p>\n<p>1. New project -> c# -> empty project<br \/>\n2. Enter project name (e.g. hellodlltest)<br \/>\n3. Right click on the project name ( in the solution explorer) add-> new item<br \/>\nselect class and enter a filename<br \/>\n4. Copy and paste the code and then compile. (you may need to change the Dllimport to the dll file name that has been created from the c dll project)<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\nusing System.Runtime.InteropServices;     \/\/ DLL support\r\n\r\nnamespace hellodlltest\r\n{\r\n    class hellodllC\r\n    {\r\n        [DllImport(\"hellocdll.dll\")]\r\n        public static extern void DisplayMessageFromDLL();\r\n\r\n        [DllImport(\"hellocdll.dll\")]\r\n        public static extern int DisplayValueAndReturn(int i);\r\n\r\n        static void Main ()\r\n           {\r\n                  Console.WriteLine (\"C# program 'talking' to an C DLL\");\r\n            DisplayMessageFromDLL();\r\n            Console.WriteLine(DisplayValueAndReturn(3).ToString());\r\n           Console.ReadLine()\r\n           }\r\n    }\r\n}\r\n<\/pre>\n<p>5. Copy the dll file from the debug directory of the c++ created dll project and place into the created bin\/debug directory for this c# project<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because there is a few things that c# cannot do compared to c++, e.g. cpu\/memory management. And also there is probably allot of dll&#8217;s out there are still required for some events, c# is able to communicate with these files and use there functions within the c# language. To create an dll within Visual Studio &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/c-dll-objects-accessed-from-c\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++ DLL Objects accessed from C#<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[14,10,9,8,398,13,400],"class_list":["post-181","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","category-c_sharp","tag-c-sharp","tag-c","tag-gcc","tag-gnu","tag-linux","tag-mono","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=181"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}