Questions tagged [pinvoke]
P/Invoke is an implementation specification created by Microsoft of the Common Language Infrastructure (CLI) for invocation of native code libraries from managed code.
3,761
questions
-1
votes
1
answer
71
views
Migrating code to C#/Win32 causes exceptions
In our (WinUI3-based) project, we've been using the P/Invoke packages to call low-level methods that modify the windows. But recently all these packages have been deprecated in favor of the source-...
1
vote
1
answer
34
views
Using LibraryImport to import same function in multiple dlls
I am creating a .net WebAPI to facilitate a gradual move from Delphi code to C#, but our codebase is very large at 1.5M+ lines of code. So we are trying to break our application into small libraries ...
1
vote
0
answers
52
views
How to get a window handle for error output in .NET Core 6 compared to .NET Framework in Visual Studio 2022
For context :
I am in charge of upgrading a service from .NET Framework 4.7.2 to .NET Core 6. This service is called by a master service that start some others services. This master service is the ...
-1
votes
0
answers
62
views
How to setup a singleton log mechanism for native library [duplicate]
I am interfacing to a C library which dispatch log messages to a callback.
I am having a hard time understanding how to interface with the logger mechanism in c# (using Microsoft.Extensions.Logging).
...
1
vote
1
answer
52
views
How to Marshal data in a double-nested structure
The following code has been simplified, but embodies the general functionality. It currently works with just Struct1 and Struct2, but I need to add the third structure. The issue is that the size of ...
1
vote
1
answer
46
views
PowerShell: How to [ref] an element in array?
I'm working on coding a P/Invoke using PowerShell Core, but it fails.
using System;
using System.Runtime.InteropServices;
public class Bindings
{
[DllImport("MyEncoder.dll")]
public ...
-1
votes
2
answers
97
views
How to pass array pointer using PInvoke from C# to C library
I am writing a software in C# which needs to call a function from a C library that is provided as a dll. Inside this library, there exists a function called
getData(uint16_t *buffer, const uint16_t ...
-1
votes
1
answer
43
views
Detect application-specific user activity within Visual Studio Extension (VSIX)
I would like to programmatically detect when a user is interacting with Visual Studio from within a Visual Studio extension (VSIX). I do not want any keypress or mouse event information, just a ...
2
votes
0
answers
63
views
How to get Outlook Property Names using extended MAPI GetNamesFromIDs
I am trying to view property names from an Outlook message using extended MAPI, ie the contents of a MAPINAMEID structure.
I get a list of property tags for a message and then I am passing the below ...
-1
votes
1
answer
43
views
how to passing c++ tcp communication data to c# with p/invoke
I would like to continue sending data received from C++(not C++/CLI) to C# via TCP communication.
I wrote code like this:
C++
LibMain.h
#pragma once
#include <string.h>
#include <...
0
votes
0
answers
92
views
How do I set CRYPT_FORCE_KEY_PROTECTION_HIGH to enforce high-level strong private key protection for RSACryptoServiceProvider?
EDIT: I was passing CRYPT_FORCE_KEY_PROTECTION_HIGH to the wrong function as pointed out by Luke. I also had an incorrect value for the flag. After making both these corrections the user gets a pop-up ...
1
vote
1
answer
46
views
PInvoike InternetGetCookieEx2 returns Error 12006
I am trying to retrieve cookies using c#, but I get an error 12006 complaining about the URL. I use InternetGetCookieEx successfully to retrieve cookie data, but I need to retrieve the entire cookie ...
-1
votes
2
answers
115
views
How to marshal this c struct and call its method in c#
I've seen answers similar to this in other places, but none of those solutions are working and usually they are slightly different questions. I haven't seen any of the other questions' solutions help ...
-1
votes
1
answer
46
views
PInvoke: Struggle to set up struct with a buffer pointer which could take various types depending on context
I am trying to build C# client for IBM TSM Api (tivoli storage manager) represented by tsmapi64.dll
For reference I have api documentation and also a sample application (source code + headers), ...
1
vote
3
answers
120
views
Interop C# IntPtr to C void* to C++ PCCERT_CONTEXT and back again
I'm needing to use C++ to get a certificate from the local machine store because Unity & Mono don't support the local machine store correctly.
To that end I've implemented the following, but my ...
2
votes
2
answers
142
views
Marshaling an array of structs with `char` array fields in F#
The Csound API defines the following CS_AUDIODEVICE struct:
typedef struct {
char device_name[64];
char device_id[64];
char rt_module[64];
int max_nchnls;
int isOutput;
}...
0
votes
1
answer
488
views
Unable to load DLL "Module could not be found HRESULT: 0x8007007E"
I have C# WPF application in which I have used C++ DLL libraries. When I build my project it builds successfully and all my DLL and exe are stored in bin/(Platform) eg(bin/Debug) folder.
Now if I copy ...
-1
votes
1
answer
55
views
C# - Problem passing a void* attribute of a struct to a C DLL
I have a C (not C++, as is most common) function in a DLL with the following signature:
unsigned long MyFunc(mystruct *arr, unsigned long arrLen);
where arr is an array of mystruct and arrLen is the ...
-1
votes
1
answer
201
views
Marshalling LPDWORD into C# for P/Invoke
I have a C++ function (WinAPI) with the following signature:
__declspec(dllimport) LPDWORD WINAPI MyFunction(HDET hDet, WORD wStartChan, WORD wNumChans,
LPDWORD lpdwBuffer, LPWORD lpwRetChans, ...
0
votes
1
answer
73
views
How to p/Invoke CFDictionaryGetValue?
I'm trying to get a list of windows in my C# app on macOS. I have the following code:
public static class Native
{
public static uint kCGWindowListOptionOnScreenOnly = 1;
public static uint ...
1
vote
1
answer
68
views
C# call C DLL - Difference between passing arguments to DLL using pointers manually and using automatic marshalling by P/Invoke
I realise there are many questions related to this one. I am able to follow them and get functional code, but I don't understand how it works or which way is better. I'm afraid this question might be ...
-2
votes
1
answer
100
views
P/Invoke UTF-8 string not being passed correctly and corrupting heap
I am trying to use a C dll within a .Net Framework application using P/Invoke. Here is the signature of the function as declared in the C source:
/* extern "C" */ __declspec(dllimport) int ...
1
vote
1
answer
118
views
C# PInvoke NtQueryObject Field CreationTime in ObjectBasicInformation struct is always 0
OK, let's get straight down to the point.
I'm trying to call NtQueryObject function in ntdll.dll to retrieve ObjectBasicInformation
I'll paste my definitions here:
[DllImport("ntdll.dll", ...
-3
votes
1
answer
148
views
Disable Windows taskbar menu for a WPF window [closed]
I need to disable Windows taskbar system menu (when you hover over a window on taskbar):
I want it either to be not visible or all the items grayed out or click on any item (most important - minimize ...
0
votes
0
answers
28
views
SWIG & MarshalDirectiveException: Marshaling to and from COM interface pointers isn't supported
I'm using SWIG to store C# managed callback functions inside an unmanaged class. Then, I'm calling an unmanaged method (say, e.g., Execute()) that will call all managed functions passed earlier. ...
-1
votes
1
answer
249
views
How do I pInvoke CM_Register_Notification from C#?
I've found CM_Register_Notification which seems to be the simplest way to get notified of device addition and removal as mentioned in the first note here.
I've even found this which explains how to do ...
-2
votes
1
answer
109
views
Access Violation error when PInvoke C function calls Win32 function that allocates memory
I had written a library (which worked fine) in C# for running Win32 functions. Such as PInvoking CredUIPromptForWindowsCredentials,CredUnPackAuthenticationBuffer,CredPackAuthenticationBuffer, so one ...
-2
votes
1
answer
84
views
C++ DLL returns unexpected array values when called from C# using P/Invoke
I'm creating a C++ DLL library to be used in a C# application through P/Invoke. The C++ library exposes a function like this:
int getData(unsigned char *ptr) {
ptr = new unsigned char[5];
ptr[...
0
votes
1
answer
62
views
struct passed by in ref to foreign function has unset fields
c header extract:
struct GetArgvOptions {
uint skip;
pid_t pid;
bool nuls;
};
bool get_argv_of_pid(const struct GetArgvOptions* options, struct ArgvResult* result);
c# classlib ...
-1
votes
1
answer
459
views
P/Invoke with LibraryImport, problem with byte array and its in/out length attributes marshalling
I have a function defined with the old [DllImport] as follows and it works nicely
[DllImport("tbs.dll", EntryPoint = "Tbsip_Submit_Command", CharSet = CharSet.Unicode, ...
-1
votes
2
answers
287
views
Passing .NET string to UTF-8 char pointer and size C function
I am trying to interop with a C API that looks like this:
bool next(state* state, const uint8_t* input, size_t input_length, result* result);
input should be a pointer to an array of UTF-8 bytes and ...
-2
votes
1
answer
59
views
C# - natively create a form window as a child to an existing window
I have a C# (4.72) library that creates a window using native API’s (pinvoke). I inserted this window into an existing application successfully. So, I have a child window inserted into the existing ...
2
votes
1
answer
274
views
Dark System ContextMenu in Window
i am using winui 3 app, when i switch theme to Dark mode, system contextMenu (Right Click on Titlebar) is not updated to Dark, so i need to use
[DllImport("uxtheme.dll", EntryPoint = "#...
0
votes
0
answers
199
views
Fix GetFirmwareEnvironmentVariable function usage in VB.NET
I'm trying to implement GetFirmwareEnvironmentVariable function. I think my implementation is wrong, because I always receive a string of unintelligible characters except for the PlatformLang and ...
-1
votes
1
answer
470
views
Load Native AOT C# dll with extern
I have a C# DLL that I am compiling to Native AOT. The content of the DLL is as follows:
namespace teste
{
public class Principal
{
public Principal()
{
}
...
-1
votes
1
answer
116
views
I get an error while invoking C# code via PowerShell 5.1
There's a code for checking the Focus Assist status on GitHub
The code works perfectly and is compilable. But unfortunately it doesn't work using P/Invoke in PowerShell 5.1 at all (on PowerShell 7.3 ...
-1
votes
1
answer
287
views
How do I use native (p/invoke) overlapped IO from C# utilizing async/await?
I'd like to make use of native overlapped IO methods (via P/Invoke) in C# in an async/await friendly manner.
The following give good instructions on how to use overlapped IO in general:
Using ...
1
vote
1
answer
135
views
C++ to F# bindings problems on Windows
Here is my problem:
I need to design a program in F# that, from a C++ program, performs simple 3D vector calculations. I don't share the calculations on the codes below because the problem stops at ...
-1
votes
2
answers
346
views
C# DllImport - Call a function that returns an struct
I am working in C# and I need to call a function in a C++ dll library. This function returns a struct but I can´t get anything.
This is the function I need to call and the struct that returns in C++ ...
0
votes
1
answer
107
views
As a C# developer, how do I find out whether a DLL method needs to be P/Invoked with stdcall or with cdecl calling convention?
Consider the following two (randomly chosen, just because I happen to use both of them in the project that I have currently open) example API calls, both contained in Microsoft libraries:
...
4
votes
1
answer
2k
views
Marshalling Function Pointers with .NET 7 LibraryImport
I'm trying to implement some P/Invoke code using the new LibraryImport attribute, as opposed to the old DllImport. Specifically, I am trying to marshal a WNDCLASSEXW struct for use in RegisterClassEx.
...
-1
votes
1
answer
313
views
Getting Wifi RSSI or signal strength using WlanAPI and IpHlpAPI [closed]
I'm trying to recreate the functionality of the wifi icon inside the Windows task bar using this library and C#.
https://github.com/dahall/Vanara
To get the current network interface I am calling ...
0
votes
0
answers
140
views
Are the restrictions on native API gone for WinUI 3 vs UWP?
For UWP I always used this helpful page to reference the things I can and cannot use. Note that APIs outside of this list actually do function on desktop, but they are rejected from the store for ...
-1
votes
1
answer
130
views
Trying to create a demo using RegenerateUserEnvironment throwing Access violation error
I am trying to make use of RegenerateUserEnvironment method so that I can refresh environment variables without restarting the instance. However, it ends up with an access violation error. I am not ...
1
vote
0
answers
203
views
How to include Pinvoked shared libraries in a single file publish app?
Our C# app currently has a native C shared library from which we call a couple of functions using the Pinvoke "DLLImport". The issue is, we haven't found a way to include the shared library ...
-1
votes
1
answer
114
views
Best way to craft an AnyCPU assembly using either 32-bit or 64-bit native DLLs?
I generated bindings for a native library but realized that imports differ between 32-bit and 64-bit.
Problem:
Not only the entry point differ, but also the calling convention:
32-bit import:
[...
1
vote
2
answers
188
views
Equivalent of Marshal.PtrToStructure in Unsafe class?
None of the methods in Unsafe class do work when trying to read a structure from a pointer.
They all display Unable to read memory in the debugger:
var info1 = Marshal.PtrToStructure<DbgHelp....
-3
votes
1
answer
189
views
Wrapping c++ class so I can use it in c#
I have this c++ header:
// BRepPrimAPI_MakeSphere.hxx file
#include <Standard.hxx>
#include <Standard_DefineAlloc.hxx>
#include <BRepPrim_Sphere.hxx>
#include <...
-1
votes
1
answer
77
views
C# P/Invoke Marshaling variable-length Array of struct
I want to call a function of a library from Net6 C#. The function expects a pointer to a structure. Inside the structure is a variable-length array. I don´t know how to marshal this array correctly.
...
-1
votes
1
answer
335
views
PInvoke C++ dll from C# throws Exception
The project I am working on has a case where I have to read and display hardware info, for which they have a function written in a C++ DLL, and I am writing a C# stub to PInvoke it. I am fairly new to ...