dllimport


MarshalDirectiveException


I would like use unmanaged code from C in C#.
I built a DLL with C code with this functions:
struct GetPluginData
{
int data[22];
};
DLLEXPORT extern "C" __declspec (dllexport) GetPluginData GetDataArray(int number);
In C# I've got this code:
[StructLayoutAttribute(LayoutKind.Sequential, Pack=1)]
public unsafe struct GetPluginData
{
/// int[22]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 22, ArraySubType = UnmanagedType.I4)]
public int[] data;
}
[DllImport("RBRPlugin.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "GetDataArray")]
[return: MarshalAs(UnmanagedType.Struct)]
public static extern GetPluginData GetDataArray(int number);
In button event or othe place code, I wrote this:
GetPluginData tes = GetDataArray(1);
And I'm getting the error: The type signature of this method is not PInvoke compatible.
I'm looking for information throught google, but no result found...
Thanks in advance!
Problems:
CallingConvertion.StdCall is incorrect, you didn't declare the function __stdcall in the C code. Make it CallingConvention.Cdecl.
Remove the [return: MarshalAs(UnmanagedType.Struct)] attribute, it is incorrect. UnmanagedType.Struct is rather unintuitive and not actually used on structs.
Remove the Pack attribute property, it is very unlikely to be correct albeit harmless here. Default packing in the C compiler is 8 unless you used #pragma pack or the /Zp compile option.
The pinvoke marshaller doesn't support returning a struct as a function return value that contains an embedded array. The source of the exception. Declare it as fixed int data[22] instead.
Here better, the correct code is:
[DllImport("RBRPlugin.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "GetDataArray")]
public static extern GetPluginData GetDataArray(int number);
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct GetPluginData
{
public unsafe fixed int data[22];
}
public GetPluginData temp1 = new GetPluginData();
private void button1_Click(object sender, EventArgs e)
{
GetPluginData test = GetDataArray(1);
temp1 = test;
unsafe
{
fixed (GetPluginData* p = &temp1)
{
p->data[0] = 1;
}
}
}

Related Links

DllImport is not found C++
Mangled member function name different during dllimport
how to use DllImport in metro-style app
Converting doc file to post script
How can I get a COM component which can be used on Windows phone 8 project with C#
Externs and DLL imports
Mixed native and managed code heap corruption
MarshalDirectiveException
call unmanaged C++ code from C# using pinvoke
Detecting DLL Code Splicing
WOW64 Redirection and LoadLibrary
Using C# dll in Windows phone 7
Python 2.5 Import dll AttributeError
Replacing the Import Table in PE file by standart LoadLibrary
Unresolved external symbol
Declare and <DllImport> in VB.NET have different results

Categories

HOME
service-worker
oracle12c
cheerio
codenvy
ecmascript-6
gpu
case
fftw
package
r-googlesheets
alarm
xbee
hpc
boto3
lanczos
okhttp
fosrestbundle
octave
vimdiff
endeca
appcelerator-studio
startup
nancy
do-while
imessage
google-maps-autocomplete
symbols
roslyn
joptionpane
expressionengine3
definitelytyped
pylons
appirater
nstimer
wcag
openmdao
realm-mobile-platform
boost-asio
google-cast
mobx
angularjs-material
card.io
gnat-gps
baidu-map
engine.io
ideascript
hackintosh
comparison-operators
android-softkeyboard
linqpad
bindingnavigator
android-checkbox
dotnet-httpclient
user-defined-fields
appdomain
headless
check-mk
mach-o
janus
flac
vcloud-director-rest-api
visual-studio-2008-sp1
docpad
pure-css
pacemaker
greatest-n-per-group
macaulay2
simevents
aspen
state-restoration
toran-proxy
.bash-profile
sapscript
dronekit-android
ocra
linked-tables
bufferedinputstream
zend-currency
diff3
j-security-check
webfinger
genymotion-call
named-parameters
build-script
twitter-bootstrap-rails
spring-3
eclipse-kepler
hidapi
merb
asplinkbutton
saleslogix
extconf.rb
entity-attribute-value
mysql-error-1205
webdev.webserver
luajava
maven-1
drupal-fivestar
binomial-heap
hbm2java
dsl-tools
xpsdocument
scrubyt
change-management

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App