6 Replies - 681 Views - Last Post: 20 January 2015 - 09:27 PM Rate Topic: -----

#1 Rhino1111   User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Is it possible to use .h function from C#?

Posted 20 January 2015 - 04:46 PM

I've tried to google my problem for a possible solution to no avail so far. Basically, I'm in a situation where I need to call a macro in Windows.h from C#. Is there any way to do it? Managed C++ wrapper? Something like that? (I have no C++ knowledge, so I'd need to read up a bit on C++ if it's possible (writing a wrapper to a single function shouldn't be too hard, right? :P)).

It's this function: https://msdn.microso...v=vs.85%29.aspx

What's the best way for me to go about this? lol.

Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to use .h function from C#?

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

Re: Is it possible to use .h function from C#?

Posted 20 January 2015 - 04:52 PM

I am not sure what you have been searching but "C# call C++ function" yields lots of results, including this one which has a number of links, and examples of creating a wrapper.

That page also links to this blog.

This post has been edited by andrewsw: 20 January 2015 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 Rhino1111   User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: Is it possible to use .h function from C#?

Posted 20 January 2015 - 04:57 PM

That might have been my problem, I kept trying for things along the lines of "Call .h function from C#". That blog link looks promising, I'll post back if I run into any problems or have questions. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

Re: Is it possible to use .h function from C#?

Posted 20 January 2015 - 05:02 PM

Mm that function/macro returns a fiber, which is:

Quote

A fiber is a unit of execution that must be manually scheduled by the application. Fibers run in the context of the threads that schedule them.

I know little about these but I'm suspicious whether this will return a useful value, given that the application that is running is your C# application.

Still, I'm sure you know what you are doing ;)
Was This Post Helpful? 0
  • +
  • -

#5 Rhino1111   User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: Is it possible to use .h function from C#?

Posted 20 January 2015 - 05:52 PM

Yep, I'm writing a coroutine system using fibers. The "CreateFiber(...)" function in Kernel32.dll returns an IntPtr to the fiber, so I'm interested in the fiber pointer with the macro call. I'm mainly stumped on the c++ end of things because I have no idea what I'm doing.

I have a question, The GetCurrentFiber() macro returns a PVOID, with the definition below, the "PVOID CurrentFiber()" function is returning a void* to my C# code. Is there any way to marshal the PVOID to an IntPtr before returning it? I couldn't find anything again...

// Fibercpp.h

#pragma once
#include <windows.h>
using namespace System;

namespace Fibercpp {

	public ref class Fibers
	{
	private:
	public:
		PVOID CurrentFiber()
		{
			return GetCurrentFiber();
		}
	};
	
}


Thanks again.
Was This Post Helpful? 0
  • +
  • -

#6 Rhino1111   User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: Is it possible to use .h function from C#?

Posted 20 January 2015 - 06:17 PM

Sorry for the double post, Didn't realize IntPtr took a void* as a constructor argument. this is what I ended up with.

IntPtr CurrentFiber()
		{
			PVOID ptr = GetCurrentFiber();
			return IntPtr((void*)ptr);
		}


Do I need to delete ptr; after constructing the IntPtr? or would that delete the Fiber? lmao.

This post has been edited by Rhino1111: 20 January 2015 - 06:25 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Is it possible to use .h function from C#?

Posted 20 January 2015 - 09:27 PM

Do an experiment and find out the result.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1