Jump to content

C dll in C#

- - - - -

  • Please log in to reply
17 replies to this topic

#1
Alexxx888

Alexxx888

    Newbie

  • Members
  • PipPip
  • 14 posts
Hello! I am really clued how to import a code written in C (with its own functions) to C# windows form project. My idea is to to pass arguments to the functions in the C.dll from C# code. For example i have some buttons and when they are triggered i want to pass some arguments (array and others) to a sorting function in the C.dll.

I was googling and found Using C DLLs in C# (Visual Studio 2008) but when i tried to execute the code to see how it works i got the message add doesnt exist in the current context. ( using Visual Studio 2010)I

i am still novice and learning C,C++ and C# and i know that what i am trying to achieve is stupid, but i want to be able to run C code with windows forms.

Thanks in advance!

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Have you added the DLL as a reference to the project? I'm guessing that the dll was not moved into the bin directory of your C# application. Open the bin directory to make sure it is there. If not, copy it over.

#3
Alexxx888

Alexxx888

    Newbie

  • Members
  • PipPip
  • 14 posts
I have it there and got the same error. :X

#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Are you using .NET 4? If so,

[DllImport("YourDll.dll", CallingConvention = CallingConvention.Cdecl)]

#5
Alexxx888

Alexxx888

    Newbie

  • Members
  • PipPip
  • 14 posts
Tried still not compiling ... Have the using System.Runtime.InteropServices; and copied the program from the site.. Interesting/

#6
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Wait. It's not compiling? Did you compile the dll first, then copy it into the C# project's bin?

Can you post your code?

#7
Alexxx888

Alexxx888

    Newbie

  • Members
  • PipPip
  • 14 posts
Build the project(dll one) with the _declspec(dllexport) and stuff-> went to his release folder copied the DLL and paste in C# bin folder. And the C# project cant be run/compiled

#8
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
I'm sorry if this sounds stupid, but by default, VS starts with DEBUG as the default build. Is that set to release?

#9
Alexxx888

Alexxx888

    Newbie

  • Members
  • PipPip
  • 14 posts
" OK, now after you build the project, you'll have a testdll.dll file in the project folder. This is the DLL file that we'll be copying to the C# Project folder. "

Build-> Build solution (creates the dll in the release folder)

#10
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
yes, copy everything from the c++ bin directory into the C# project's bin directory (under debug or release)

#11
Alexxx888

Alexxx888

    Newbie

  • Members
  • PipPip
  • 14 posts
Thanks for the help lespauled !!! Pulled it off at last. OK first step done so i need to know how to manipulate forms - set background image- logo. And again ty vm.

#12
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Anytime.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users