Avatar billede tofte Juniormester
04. juni 2016 - 08:20 Der er 7 kommentarer

Kald COM dll - problemer med Variant type

Hej

Jeg har brug for at kalde en COM dll fra .net. Jeg kan sætte de fleste parametre - mest double, og køre de fleste metoder, men jeg ike finde ud af at sætte den siste parameter, da denne skal være Variant.

Metoden DM.set_iPassword forventer et Object og i følge programmøren og en DLL viewer skal det være af typen Variant hvordan koverterer jeg string til variant.

Jeg kan fremsende DLL mv. hvis set er nødvendig.

Et simpel eksempel ses nedenfor:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DesMod;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
class Program
{
static private double fromCToF(double C)
{
return (1.8 * C + 32);
}

static void Main(string[] args)
{
System.Console.WriteLine("Start");
clsDesMod DM = new clsDesMod();
DM.iDLL = true;
String pw = "1234";

//this line is wrong it is expecting a variant(Object) but I can not manage to cast/wrap the password string to a variant.
DM.set_iPassword(pw);

DM.iRotorDepthmm = 200;
DM.iRPH = 5;
DM.iElevation = 0;
DM.iSect2Fraction = 90;
DM.iSect1Ti = fromCToF(20);
DM.iSect1Wi = 30;
DM.iSect1Vel = 300;

DM.iSect2Ti = fromCToF(120);
DM.iSect2Wi = 30;
DM.iSect2Vel = 300;

DM.Calculate();
double rs = DM.oDeltaP1;
bool pwok = DM.oPasswordOK;


}


}
}
Avatar billede arne_v Ekspert
04. juni 2016 - 14:34 #1
Har du proevet:

DM.set_iPassword(new BStrWrapper(pw));

?
Avatar billede tofte Juniormester
04. juni 2016 - 15:44 #2
Ja, den giver desværre også Type Mismatch.

Filen ligger her:
hx.cotes.com/DesMod160.zip

Jeg er lidt usikker på, hvad den er lave i, men installeren og designet leder tankerne hen på VB6 eller noget C++ fra samme tidsalder. Af installeren fremgår der "Setup Bootstrap for Visual Basic Toolkit". Filen og programmet er nyt - jeg troede slet ikke man kunne få fat i sådan noget længere,
Avatar billede arne_v Ekspert
05. juni 2016 - 01:15 #3
Fortaeller DLL viewer et namespace paa den Variant type?
Avatar billede tofte Juniormester
05. juni 2016 - 07:19 #4
Jeg ved ikke helt, er ikke så rutineret. Men nedenstående er hvad jeg kan hive ud.

//Version: 32.0
DesMod;
GUID = {81883466-E918-4848-AF60-ADD563AF6A15};


Dispatch _clsDesMod;
GUID = {4D7DA335-139B-495A-B6C8-9451D385F871};
  function QueryInterface(riid: ^GUID; out ppvObj: ^^VOID); stdcall;
  function AddRef: UI4; stdcall;
  function Release: UI4; stdcall;
  function GetTypeInfoCount(out pctinfo: ^UINT); stdcall;
  function GetTypeInfo(itinfo: UINT; lcid: UI4; out pptinfo: ^^VOID); stdcall;
  function GetIDsOfNames(riid: ^GUID; rgszNames: ^^I1; cNames: UINT; lcid: UI4; out rgdispid: ^I4); stdcall;
  function Invoke(dispidMember: I4; riid: ^GUID; lcid: UI4; wFlags: UI2; pdispparams: ^DISPPARAMS; out pvarResult: ^Variant; out pexcepinfo: ^EXCEPINFO; out puArgErr: ^UINT); stdcall;
  property-get iDLL: Bool; stdcall;
  property-put iDLL(: Bool); stdcall;
  property-get iPurgeReact: Bool; stdcall;
  property-put iPurgeReact(: Bool); stdcall;
  property-get iSect1Ti: R8; stdcall;
  property-put iSect1Ti(: R8); stdcall;
  property-get iSect1Wi: R8; stdcall;
  property-put iSect1Wi(: R8); stdcall;
  property-get iSect1Vel: R8; stdcall;
  property-put iSect1Vel(: R8); stdcall;
  property-get iSect1Fraction: R8; stdcall;
  property-put iSect1Fraction(: R8); stdcall;
  property-get iSect2Ti: R8; stdcall;
  property-put iSect2Ti(: R8); stdcall;
  property-get iSect2Wi: R8; stdcall;
  property-put iSect2Wi(: R8); stdcall;
  property-get iSect2Vel: R8; stdcall;
  property-put iSect2Vel(: R8); stdcall;
  property-get iSect2Fraction: R8; stdcall;
  property-put iSect2Fraction(: R8); stdcall;
  property-get iSect3Ti: R8; stdcall;
  property-put iSect3Ti(: R8); stdcall;
  property-get iSect3Wi: R8; stdcall;
  property-put iSect3Wi(: R8); stdcall;
  property-get iSect3Vel: R8; stdcall;
  property-put iSect3Vel(: R8); stdcall;
  property-get iSect3Fraction: R8; stdcall;
  property-put iSect3Fraction(: R8); stdcall;
  property-get iRPH: R8; stdcall;
  property-put iRPH(: R8); stdcall;
  property-get iRotorDepthmm: R8; stdcall;
  property-put iRotorDepthmm(: R8); stdcall;
  property-get iElevation: R8; stdcall;
  property-put iElevation(: R8); stdcall;
  property-get oRPH: R8; stdcall;
  property-put oRPH(: R8); stdcall;
  property-get oSect1Tout: R8; stdcall;
  property-put oSect1Tout(: R8); stdcall;
  property-get oSect1Wout: R8; stdcall;
  property-put oSect1Wout(: R8); stdcall;
  property-get oSect1BTUlbout: R8; stdcall;
  property-put oSect1BTUlbout(: R8); stdcall;
  property-get oSect2Tout: R8; stdcall;
  property-put oSect2Tout(: R8); stdcall;
  property-get oSect2Wout: R8; stdcall;
  property-put oSect2Wout(: R8); stdcall;
  property-get oSect2BTUlbout: R8; stdcall;
  property-put oSect2BTUlbout(: R8); stdcall;
  property-get oSect3Tout: R8; stdcall;
  property-put oSect3Tout(: R8); stdcall;
  property-get oSect3Wout: R8; stdcall;
  property-put oSect3Wout(: R8); stdcall;
  property-get oSect3BTUlbout: R8; stdcall;
  property-put oSect3BTUlbout(: R8); stdcall;
  property-get oDewPtTemp1: R8; stdcall;
  property-put oDewPtTemp1(: R8); stdcall;
  property-get oDewPtTemp2: R8; stdcall;
  property-put oDewPtTemp2(: R8); stdcall;
  property-get oDewPtTemp3: R8; stdcall;
  property-put oDewPtTemp3(: R8); stdcall;
  property-get oWbalance: R8; stdcall;
  property-put oWbalance(: R8); stdcall;
  property-get oTbalance: R8; stdcall;
  property-put oTbalance(: R8); stdcall;
  property-get oDeltaP1: R8; stdcall;
  property-put oDeltaP1(: R8); stdcall;
  property-get oDeltaP2: R8; stdcall;
  property-put oDeltaP2(: R8); stdcall;
  property-get oDeltaP3: R8; stdcall;
  property-put oDeltaP3(: R8); stdcall;
  property-get iPassword: Variant; stdcall;
  property-put iPassword(: ^Variant); stdcall;
  property-putref iPassword(: ^Variant); stdcall;
  property-get oError: BSTR; stdcall;
  property-put oError(: BSTR); stdcall;
  property-get oPasswordOK: Bool; stdcall;
  property-put oPasswordOK(: Bool); stdcall;
  function Calculate; stdcall;
  function GoodPassword; stdcall;

CoClass clsDesMod;
GUID = {0343172F-D8F2-4895-9563-73D8056D5964};
Avatar billede tofte Juniormester
05. juni 2016 - 07:26 #5
Jeg har også prøvet:
String pw = "1234";
IntPtr pVariant =Marshal.StringToBSTR(pw);
object objRet = Marshal.GetObjectForNativeVariant(pVariant);
DM.set_iPassword(objRet);
Så giver den:
           
Managed Debugging Assistant 'InvalidVariant' has detected a problem in 'C:\Users\RTO\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\ConsoleApplication2.vshost.exe'.

Additional information: Der blev registreret en ugyldig VARIANT under en samtale fra en ikke-administreret VARIANT til et administreret objekt. Overførsel af ugyldige  VARIANT'er til CLR kan medføre uventede undtagelser, beskadigelse eller tab af data.
Avatar billede arne_v Ekspert
05. juni 2016 - 14:24 #6
Det DLL view er af den rigtige COM DLL.

Men syntaxen:

clsDesMod DM = new clsDesMod();

plejer at betyde at der er blevet genereret en .NET wrapper DLL.

Og det var den jeg jeg taenkte paa.
Avatar billede tofte Juniormester
05. juni 2016 - 19:33 #7
Tak for hjælpen, jeg har fundet min gamle VBS6.0 CD frem og så laver jeg en wrapper dll som kan trække koderodet et andet sted fra. En smule ufleksibelt men det heldigvis ikke alle som stadig kun udvikler til VB6.0
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester