Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a C# delegate's calling convention to CDECL

Tags:

I have had this problem with C# when I was using DotNet1.1

The problem is this. I have an unmanaged dll, which has a function which takes a function pointer (among other arguments). When I declare the DLLImport in C# code, I pass a delegate. But the delegates in C# have stdcall calling convention whereas the unmanaged function expects a cdecl function pointer. Thus my naive approach resulted in crashes. Then I found the following: http://www.codeproject.com/KB/cs/cdeclcallback.aspx Some guy wrote an excellent library that enables changing calling convention of the delegate by, as I understood, MSIL-hacking. Things went well, until...

I migrated to VS2008 and the new version of .NET. Under this version the abovementioned library doesn't work. I am not really a C# or .NET expert, and, to tell the truth, I barely understand what his library does (although it's open-source), so I don't even want to try to adapt it to new .NET. However I am hoping that newer version of C# has some better solution available for my problem.

So, SO experts, please help me with my pain in the buttocks :)

like image 864
Armen Tsirunyan Avatar asked Mar 01 '11 13:03

Armen Tsirunyan


People also ask

Can I replace my AC by myself?

Simply put, no, you cannot replace your AC unit yourself. Even if you have the technical know-how to install an AC unit, all of the electrical components add an elevated level of risk to the process. Plus, it takes nuanced HVAC experience to ensure you get the right unit for the size of your house.

How much does it cost to change an AC system?

The cost of replacement for an air conditioner in a residential home can range between $4,350-$12,095. This includes labor and permit fees. This range also covers a variety of system sizes and levels of sophistication, including variable-speed AC systems.

How often should you change your AC?

If you kept your air conditioner in good condition over its lifetime, expect to need a replacement in about 10-15 years. Keep in mind that as your A/C grows older, its mechanical parts can become unavailable or obsolete over time. If you aren't able to replace a broken part, you will need to purchase new unit.

Should I replace my 13 year old AC?

Age of the UnitEnergyStar recommends replacing your AC unit every 10 to 15 years. If you have an eight-year-old or older air conditioning system, it may not be worth the cost to repair it unless the repair is an easy fix, like a worn fan belt or a clogged condenser unit.


1 Answers

By default the p/invoke system wraps your delegate in a stdcall function. You can change the generated wrapper's calling convention by using the UnmanagedFunctionPointer attribute:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void MyDelegate(); 
like image 180
David Heffernan Avatar answered Oct 21 '22 14:10

David Heffernan