interop

PInvoke error when marshalling struct with a string in it

匆匆过客 提交于 2019-12-18 09:00:36
问题 I have a C++ struct struct UnmanagedStruct { char* s; // Other members }; and a C# struct struct ManagedStruct { [MarshalAs(UnmanagedType.LPStr)] string s; // Other members } the C++ library exposes extern "C" UnmanagedStruct __declspec(dllexport) foo( char* input ); And it is imported like [DllImport("SomeDLL.dll", CharSet = CharSet.Ansi)] static extern ManagedStruct foo( string input ); However when I call this function I get MarshalDirectiveException was unhandled Method's type signature

Do I need to pin an anonymous delegate?

泪湿孤枕 提交于 2019-12-18 07:48:11
问题 I am calling CopyFileEx from a C# application with an anonymous delegate being passed into the LPPROGRESS_ROUTINE parameter in order to get notifications on the file copy progress. My question is, does the anonymous delegate need to be pinned and why (or why not). In addition, does the answer change if: CopyFileEx was not blocking. If I passed in a delegate that was not anonymous. Thanks! 回答1: The delegate does not need to be pinned . A managed object is pinned if it cannot be moved by the

Arrays of strings in Fortran-C bridges using iso_c_binding

早过忘川 提交于 2019-12-18 07:36:17
问题 I'm writing code that will call a C function from Fortran using Fortran's C interoperability mechanism (introduced in Fortran 2003 and implemented in newer versions of gfortran and ifort). This answer is almost what I need, but I can't quite get my head around what interface declaration I should use in Fortran for a C function that looks like this: int use_array(int n, char * array[]){ int i; for(i=0; i<n; i++){ printf("Item %d = %s\n",i,array[i]); } return n; } I'm not clear what the

How to query GetMonitorBrightness from C#

你。 提交于 2019-12-18 06:53:49
问题 How does GetMonitorBrightness http://msdn.microsoft.com/en-us/library/ms775205.aspx work? Can someone give me an actual working implementation calling this code in C#? I'm trying to retrieve the allowed brightness levels my laptop supports. I have the following working code that sets the brightness from 1 to ~150. But I'm looking for the allowed input values (min max values). static void SetBrightness(byte targetBrightness) { ManagementScope scope = new ManagementScope("root\\WMI");

How to query GetMonitorBrightness from C#

守給你的承諾、 提交于 2019-12-18 06:52:00
问题 How does GetMonitorBrightness http://msdn.microsoft.com/en-us/library/ms775205.aspx work? Can someone give me an actual working implementation calling this code in C#? I'm trying to retrieve the allowed brightness levels my laptop supports. I have the following working code that sets the brightness from 1 to ~150. But I'm looking for the allowed input values (min max values). static void SetBrightness(byte targetBrightness) { ManagementScope scope = new ManagementScope("root\\WMI");

How can I debug a VB6 project that has a .net interop project which uses an app.config file?

点点圈 提交于 2019-12-18 05:46:12
问题 I have a .net interop project that uses an app.config file. When I am running the VB6 project that is using the interop control in Debug mode, the ConfigurationManager cannot find the app.config file. When I make the VB6 project into an exe and rename the app.config file to (VB6 binary name).exe.config, the ConfigurationManager can find the file. Is there a way to rename the app.config file or change a setting so ConfigurationManager can find the app.config file while VB6 is running in debug

C++/CLI String Conversions

女生的网名这么多〃 提交于 2019-12-18 05:18:51
问题 I found this really nice piece of code that converts a string to a System:String^ as in: System::String^ rtn = gcnew String(move.c_str()); // 'move' here is the string I'm passing rtn back to a C# program. Anyways, inside the function where this code exists, I'm passing in a System::String^ . I also found some code to convert a System:String^ to a string using the following code: pin_ptr<const wchar_t> wch = PtrToStringChars(cmd); // 'cmd' here is the System:String size_t convertedChars = 0;

Using Reflection with COM Interop

a 夏天 提交于 2019-12-18 05:15:06
问题 After an interop call, I get back a COM object. I know this object will be one of three possible COM classes (Class1, Class2, Class3), but do not know which one in runtime. The reflection upon that object (interopObject.GetType()) returns the base RCW wrapper of System.__ComObject. What I need is to set some properties on the object - Text1, Text2, ... Text30 (actual names, btw :)), which exist in all three classes. So, the question is, can I somehow get the runtime type of the object (this

In C#, how do I invoke a DLL function that returns an unmanaged structure containing a string pointer?

空扰寡人 提交于 2019-12-18 05:14:25
问题 I have been given a DLL ("InfoLookup.dll") that internally allocates structures and returns pointers to them from a lookup function. The structures contain string pointers: extern "C" { struct Info { int id; char* szName; }; Info* LookupInfo( int id ); } In C#, how can I declare the structure layout, declare the Interop call, and (assuming a non-null value is returned) utilize the string value? In other words, how do I translate the following into C#? #include "InfoLookup.h" void foo() { Info

How to Call Windows API [duplicate]

两盒软妹~` 提交于 2019-12-18 04:45:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Windows API and .net languages I want to call native Windows API from .NET Framework. I want to have an easy way where I can call native API from high-level layer like other .NET APIs. Please refer to any resource which you know. Any help will be appreciated. 回答1: You can use PInvoke in order to call Windows API functions. There is also PInvoke Interop Assistant which generates a PInvoke signature of a function