vb.net - Declaring C dll functions -
i learning make dll visual basic, made this:
dll in c
// firstdll.cpp : defines exported functions dll application. // #include "stdafx.h" #include "stdio.h" extern "c" { __declspec(dllexport) int sestej(int a, int b) { int c; c = + b; return c; } } visual basic
public class form1 declare function sestej lib "c:\users\blaž\documents\visual studio 2013\projects\firstdll\debug\firstdll.dll" (byval x integer, byval y integer) private sub button1_click(sender object, e eventargs) handles button1.click dim vnos1, vnos2, izhod integer vnos1 = convert.todecimal(vnosx.text) vnos2 = convert.todecimal(vnosy.text) sestej(vnos1, vnos2) lblvsota.text = izhod end sub end class when run error:
an unhandled exception of type 'system.runtime.interopservices.marshaldirectiveexception' occurred in windowsapplication2.exe
additional information: pinvoke restriction: cannot return variants.
i did wrong can't find anywhere.
when omit return type in vb function, assumed object. since object maps native variant type, explains error. must specify return type.
rather continue declare suggest switch p/invoke. declare how done in vb6, p/invoke .net way interop unmanaged code. declare function this:
<dllimport("...\firstdll.dll", callingconvention:=callingconvention.cdecl)> _ public shared function sestej(byval integer, _ byval b integer) integer end function this allows correct other error in code. namely calling convention mismatch. unmanaged code uses cdecl, vb code using declare uses stdcall. code above fixes that.
Comments
Post a Comment