FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DLL Help

 
Post new topic   Reply to topic     Forum Index -> Juno
View previous topic :: View next topic  
Author Message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Fri Oct 03, 2008 4:41 pm    Post subject: DLL Help Reply with quote

Greetings!

Trying to use http://www.dsource.org/projects/juno/wiki/Tutorials/DynamicInvoke, I am trying to change this VB Code
Code:


XTranslate.TranslateTask xtransApp = new XTranslate.TranslateTaskClass();

xtransApp.KeepBackup = false;

xtransApp.RestoreSourceForNotTransferredUnits = true;

xtransApp.OldBilingualFile = fm.TTXFile;

xtransApp.NewSourceFile = strTTX;

xtransApp.Execute();



which calls these functions from a DLL called TradosXTranslate.dll, and I translate the code above to D. I am trying to call DLL functions, but I am stuck on the first part of the code:

Code:
XTranslate.TranslateTask xtransApp = new XTranslate.TranslateTaskClass();


This is the code I got from running the tlbimpd on the TradosXTranslate.dll,
Code:
module xtranslate;

private import com;

interface _ITranslateTaskEvents : IDispatch {
  static GUID IID = { 0x603da819, 0xb045, 0x49df, 0x92, 0x90, 0x46, 0xd3, 0xe2, 0x68, 0xba, 0x66 };
  /+int OnProgress(in int Percent);+/
}

interface ITranslateTask : IDispatch {
  static GUID IID = { 0xd4fe6609, 0x037b, 0x4d46, 0x8b, 0xdd, 0x66, 0x26, 0x24, 0x6b, 0x40, 0x7c };
  int Execute();
  int get_SettingsFilePath(out wchar* pVal);
  int set_SettingsFilePath(in wchar* value);
  int get_NewSourceFile(out wchar* pVal);
  int set_NewSourceFile(in wchar* value);
  int get_OldBilingualFile(out wchar* pVal);
  int set_OldBilingualFile(in wchar* value);
  int get_KeepBackup(out short pVal);
  int set_KeepBackup(in short value);
  int get_BackupFile(out wchar* pVal);
  int set_BackupFile(in wchar* value);
  int get_EnableXunitColouring(out short pVal);
  int set_EnableXunitColouring(in short value);
  int get_XunitColour(out OLE_COLOR pVal);
  int set_XunitColour(in OLE_COLOR value);
  int get_RestoreSourceForNotTransferredUnits(out short pVal);
  int set_RestoreSourceForNotTransferredUnits(in short value);
  int get_TransferredUnitCount(out int pVal);
  int get_TotalUnitCount(out int pVal);
  int get_TransferAsXunits(out short pVal);
  int set_TransferAsXunits(in short value);
  int get_IgnoreDisplayFormatting(out short pVal);
  int set_IgnoreDisplayFormatting(in short value);
  int get_SettingsFile(out wchar* pVal);
  int set_SettingsFile(in wchar* value);
  int get_EnableAdvancedMatching(out short pVal);
  int set_EnableAdvancedMatching(in short value);
  int get_BrandString(out wchar* pVal);
  int get_FilterManager(out IFilterManager pVal);
  int setref_FilterManager(in IFilterManager value);
}

interface IFilterManager : IDispatch {
  static GUID IID = { 0x8f63bc55, 0x04ff, 0x4c15, 0xb5, 0x42, 0xcd, 0x1c, 0x2c, 0x5a, 0xf9, 0x0a };
  int get_FilterDefinitions(out IFilterDefinitions pVal);
  int get_FilterDefinitionsFilePath(out wchar* pVal);
  int set_FilterDefinitionsFilePath(in wchar* value);
  int get_LastChanged(out int pVal);
  int Detach();
  int MatchingFilterDefinitions(in wchar* FileName, out IFilterDefinitions pVal);
  int BestMatchingFilterDefinition(in wchar* FileName, out IFilterDefinition pVal);
  int SupportedFileTypes(out IFileTypeDefinitions pVal);
}

interface IFilterDefinitions : IDispatch {
  static GUID IID = { 0xb5bdf646, 0x0ccd, 0x4715, 0xbb, 0x71, 0xdf, 0x0b, 0x10, 0x77, 0xbd, 0x4d };
  int get_Item(in VARIANT Index, out IFilterDefinition pVal);
  int get_Count(out int pVal);
  int get__NewEnum(out IUnknown pVal);
  int Detach();
}

interface IFilterDefinition : IDispatch {
  static GUID IID = { 0xa44eac92, 0x3e5c, 0x4eed, 0xb8, 0x71, 0x20, 0x16, 0x75, 0x3d, 0x27, 0x75 };
  int get_FileTypeName(out wchar* pVal);
  int get_FileFilterRegularExpression(out wchar* pVal);
  int get_Id(out wchar* pVal);
  int get_FilePath(out wchar* pVal);
}

interface IFileTypeDefinitions : IDispatch {
  static GUID IID = { 0x7f45f8c5, 0x1c51, 0x4d0a, 0xba, 0xe2, 0x3f, 0xdd, 0xa1, 0x43, 0xc8, 0xa2 };
  int get_Item(in VARIANT Index, out IFileTypeDefinition pVal);
  int get_Count(out int pVal);
  int get__NewEnum(out IUnknown pVal);
}

interface IFileTypeDefinition : IDispatch {
  static GUID IID = { 0xaaa73e69, 0x0bd1, 0x4b3e, 0xb0, 0x8d, 0x2c, 0xd3, 0xdf, 0x2b, 0x61, 0x6b };
  int get_FileTypeName(out wchar* pVal);
  int get_FileFilterRegularExpression(out wchar* pVal);
}

interface IRunStatus : IUnknown {
  static GUID IID = { 0x87c8d4a3, 0x449c, 0x4fef, 0xa8, 0x9d, 0xa0, 0xbc, 0x77, 0x01, 0xb0, 0xcd };
  int SetParameters(in int pVal1, in int pVal2, in int pVal3, in int pVal4);
  int GetParameters(out int pVal1, out int pVal2, out int pVal3, out int pVal4);
}

interface IVersionInfo : IDispatch {
  static GUID IID = { 0xd3ad94a3, 0x14f5, 0x11d6, 0x89, 0x6b, 0x00, 0xb0, 0xd0, 0x77, 0x7f, 0x8b };
  int get_Build(out int pVal);
  int get_VersionString(out wchar* pVal);
  int get_FullVersionString(out wchar* pVal);
}

abstract class TranslateTask {
  static GUID CLSID = { 0xe69def6f, 0xe4bb, 0x4086, 0x9d, 0x54, 0xa4, 0x5a, 0xfd, 0xe7, 0x26, 0x95 };
  mixin CoClassInterfaces!(ITranslateTask, IRunStatus);
}

abstract class VersionInfo {
  static GUID CLSID = { 0xd3ad94a4, 0x14f5, 0x11d6, 0x89, 0x6b, 0x00, 0xb0, 0xd0, 0x77, 0x7f, 0x8b };
  mixin CoClassInterfaces!(IVersionInfo);
}


This is all I have,
Code:

import juno.base.native;

//import juno.com.core;
//import juno.com.client;
extern (Windows)
alias DllImport!("user32.dll", "MessageBeep", int function(uint type)) MessageBeep;

extern (Windows)
alias DllImport!("TradosXTranslate.dll", "KeepBackup", bit function()) KeepBackup;
void main()
{
  MessageBeep(-1);
  KeepBackup = false;
}


This program compiles, but I don-t think it's correct. Any help would be greatly appreciated.

thanks,

jose[/url]
Back to top
View user's profile Send private message
John



Joined: 17 Jan 2006
Posts: 75

PostPosted: Sat Oct 11, 2008 4:30 am    Post subject: Reply with quote

Hi Jose,

DllImport is for calling DLL functions dynamically, not for working with COM objects.

But you imported the type library - so you're halfway there. Here's a translation of the VB code (actually, that looks like C# to me).

Code:
import juno.com.core, xtranslate;
import bstr = juno.com.bstr; // Juno 0.4

void main() {
  // XTranslate.TranslateTask xtransApp = new XTranslate.TranslateTaskClass();
  auto xtransApp = TranslateTask.coCreate!(ITranslateTask);
  scope(exit) xtransApp.Release();

  // xtransApp.KeepBackup = false;
  xtransApp.set_KeepBackup(VARIANT_FALSE);

  // xtransApp.RestoreSourceForNotTransferredUnits = true;
  xtransApp.set_RestoreSourceForNotTransferredUnits(VARIANT_TRUE);

  // xtransApp.OldBilingualFile = fm.TTXFile;
  wchar* bstrTTXFile = bstr.fromString(fm.TTXFile); // Assuming fm.TTXFile is a D string
  xtransApp.set_OldBilingualFile(bstrTTXFile);
  bstr.free(bstrTTXFile);

  // xtransApp.NewSourceFile = strTTX;
  wchar* bstrTTX = bstr.fromString(strTTX);
  xtransApp.set_NewSourceFile(bstrTTX);
  bstr.free(bstrTTX);

  // xtransApp.Execute();
  xtransApp.Execute();
}


I'm assuming you're using the latest version of Juno. But if not, you'll need to omit the juno.com.bstr import and use toBStr/freeBStr instead.
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Sun Oct 12, 2008 10:18 pm    Post subject: Reply with quote

Thanks. Yes, it was c# code. I was looking at another VB code for the same thing, but I use the C# code instead. Is there any documentation that I can use to understand what you did?

I only have one little problem,
Quote:
0:13:15.34>build -I..;c:\dmd\import xtrans.d
xtranslate.d(53): Error: identifier 'OLE_COLOR' is not defined
xtranslate.d(53): Error: OLE_COLOR is used as a type
xtranslate.d(53): Error: cannot have parameter of type void
xtranslate.d(55): Error: identifier 'OLE_COLOR' is not defined
xtranslate.d(55): Error: OLE_COLOR is used as a type
xtranslate.d(55): Error: cannot have parameter of type void


I searched it through the internet, and I found that it is some kind of Property Data Type, http://support.microsoft.com/kb/177629. I am able to comment it out, since for now, I can, at least, compile it. So, I thank you very much.

jose
Back to top
View user's profile Send private message
John



Joined: 17 Jan 2006
Posts: 75

PostPosted: Mon Oct 13, 2008 2:53 am    Post subject: Reply with quote

Add the following line near the top of xtranslate.d:

Code:
alias uint OLE_COLOR;


Quote:
Is there any documentation that I can use to understand what you did?


It's not that different from how you'd traditionally write COM code in C++. But I can see that it might be trickier to translate C#/VB code.

The C# compliler is aware of COM types and does a lot of work under the hood during compilation. It is smart enough to know that TranslateTaskClass is a COM coclass which creates ITranslateTask objects (or rather, a special attribute attached to TranslateTaskClass tell it so). This line in C#
Code:
TranslateTask xtransApp = new TranslateTaskClass()
is merely syntactic sugar for
Code:
// (The C# equivalent of tlbimpd defines two types - ITranslateTask and TranslateTask - which are essentially the same thing. One is an interface, while the other an abstract class.)

ITranslateTask xtransApp;
CoCreateInstance(CLSID_TranslateTask, null, CLSCTX_INPROC_SERVER, IID_ITranslateTask, cast(void**)&xtransApp);


The best we can do in D is something like what Juno does: wrapping CoCreateInstance with the coCreate template to make things simpler and cleaner:
Code:
ITranslateTask xtransApp = TranslateTask.coCreate!(ITranslateTask);


Other things to be aware of are types like booleans - C# automatically converts true/false to VARIANT_TRUE/VARIANT_FALSE. The compiler also magically handles COM strings, while you need to use manage them manually with toBStr/fromBStr/freeBStr in D.

Does this help?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Juno All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group