Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String type to word type

Posted on 2004-08-23
19
Medium Priority
?
253 Views
Last Modified: 2011-09-20
 I have an activeX from Microsoft which has some functions that include a Word type variable. That variable is actually a string (like 'id=myID'). On msdn they pass that variable to the function like "id=myID", and all the info they give is that this is an OleChar variable. Thay can do that because C has almost no type checking, but Delphi has strong type checking...
  Now, I need to know how can I pass a string as a Word to this function. There must be a way to do it, maybe with pointers...
0
Comment
Question by:Bindza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
  • +3
19 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11870002
try to use PChar or PAnsichar in delphi, instead of string -> there are conversion-functions

-> just a guess -> no activeX-experience

meikl ;-)
0
 

Author Comment

by:Bindza
ID: 11870255
It's got nothing to with ActiveX in particular, I just said that so people would know what's it all about.

I forgot to add, I tried this:

var
 w: word;
 s: string;
 p: ^word;
begin
s := 'myid=SomeId';
p := @s;
w := p^;
TheMSFunction(w);
[...]

But the function returns "?" instead of "myid=SomeId".
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11870319
are you sure that not a DWord is used?

try this

TheMSFunction(integer(PChar('myid=SomeId'));


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11871069
I just think there's a flaw in the type library you're using. Which control are we talking about here?

Or perhaps it's an OVERLOADED method...
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 11871739
can you show the code you have that shows how they pass the "id=myID" to the function?

also, from the Delphi IDE, when you are in the parameters part of that function and you hit Ctrl+Shift+Space Bar, what does the parameter type say?
0
 

Author Comment

by:Bindza
ID: 11871946
It's WebBrowser that is DHTMLEd, the function (IMarkupServices.CreateElement) as MS presents it is:

HRESULT CreateElement(ELEMENT_TAG_ID tagID,
    OLECHAR* pchAttributes, // here - delphi sees it as Word
    IHTMLElement** ppElement
);
Parameters

tagID
[in] Value of type ELEMENT_TAG_ID that specifies the type of tag to create.
pchAttributes
[in] Pointer to a variable of type OLECHAR that specifies the attributes of the element.
ppElement
[out] Address of a pointer to an IHTMLElement interface that returns the newly created element.

And this is how they use it:

IMarkupServices::CreateElement ( TAGID_B, "id=anID", & pElement ) will create a new b element with the attribute IHTMLElement::id set to "anID".

This is how I tried to use it:

uses MSHTML_TLB;

procedure TForm1.Button1Click(Sender: TObject);
var
 i: IMarkupServices;
 e: IHTMLElement;
 w: word;
 s: string;
 p: ^word;
begin
//w := 0; for no attributes
s := 'src="C:\tmp\logomain.jpg"';
p := @s;
w := p^;
KSEditx1.DOM.QueryInterface(IID_IMarkupServices, i);
if i <> nil then
 begin
  i.createElement(TAGID_IMG, w, e);
  showmessage(e.outerhtml);// shows "<IMG ?>" instead of <IMG src="C:\tmp\logomain.jpg">
 end;
end;
0
 

Author Comment

by:Bindza
ID: 11871986
in WebBrowser that would be:

[...]
(WebBrowser1.Document as IHTMLDocument2).QueryInterface(IID_IMarkupServices, i);
if i <> nil then
 begin
  i.createElement(TAGID_IMG, w, e);
  showmessage(e.outerhtml);// shows "<IMG ?>" instead of <IMG src="C:\tmp\logomain.jpg">
 end;
end
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 11873028
have you tried typecasting to Word:

procedure TForm1.Button1Click(Sender: TObject);
var
 i: IMarkupServices;
 e: IHTMLElement;
 w: word;
 s: string;
 p: ^word;
begin
//w := 0; for no attributes
s := 'src="C:\tmp\logomain.jpg"';
//p := @s; //don't need this
//w := p^; //don't need this
KSEditx1.DOM.QueryInterface(IID_IMarkupServices, i);
if i <> nil then
 begin
  i.createElement(TAGID_IMG, Word(PChar(s)), e); //typecast here
  showmessage(e.outerhtml);// shows "<IMG ?>" instead of <IMG src="C:\tmp\logomain.jpg">
 end;
end;
0
 

Author Comment

by:Bindza
ID: 11874628
I think I have and it said "Invalid typecast"...
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11876774
Hello  Bindza, I'm sorry I do not have time to actually test this code before I post it, but I think I would try and just Typecaste a PWChar to a PWORD


procedure TForm1.Button1Click(Sender: TObject);
var
 i: IMarkupServices;
 e: IHTMLElement;
 s: string;
 pWCha1: PWChar;
begin
s := 'src="C:\tmp\logomain.jpg"';
pWCha1 := StringToOleStr(s);
KSEditx1.DOM.QueryInterface(IID_IMarkupServices, i);
if i <> nil then
 begin
  i.createElement(TAGID_IMG, PWORD(pWCha1)^, e);
  showmessage(e.outerhtml);
 end;
end;


but I am conpleatly dumbfounded by the Delphi code heads making a PWChar to a Word for that parameter?
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11878069
agreed with slick :-)
delphi should still import it as a pointer of some sort
if it doesn't work manually change the interface so that the param becomes a PWideChar or maybe 'var WideString'
0
 

Author Comment

by:Bindza
ID: 11879027
I'll try it.
0
 

Author Comment

by:Bindza
ID: 11880717
Thanks Slick, I tried it and it works perfectly :)
Now, if you could tell me how to convert a string like "TAGID_BUTTON" to a word like "TAGID_BUTTON" you'll get more points. I'm not really sure that this is a word, it may be an integer, in MSHTML_TLB it's defined like this:

// Constants for enum _ELEMENT_TAG_ID
type
  _ELEMENT_TAG_ID = TOleEnum;
const
  TAGID_NULL = $00000000;
  TAGID_UNKNOWN = $00000001;
  TAGID_A = $00000002;
  [...]
  TAGID_BUTTON = $00000011;

and the commands are defined like this (they are integers, obviously):

  IDM_UNKNOWN               =  0;
  IDM_ALIGNBOTTOM           =  1;
  IDM_ALIGNHORIZONTALCENTERS=  2;
  IDM_ALIGNLEFT             =  3;
  [...]

I need this so the user can specify which element he/she wants to insert, and also with the mshtml IDM_SOMETHING commands (there are so many of them that trying them all, by modifying the source and recompiling, would take forever, and I don't want a 200 line "case" or "if" statement).
0
 

Author Comment

by:Bindza
ID: 11880725
So, What do you think, Slick?
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11881859
no conversion is needed .. those consts already are integers
in delphi $0001 is a number in HEX format (in VB it's a string variable)
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11883001
???
these are all Number (Integer) constant values, and You seem to want some sore of conversion like

function convertS(aName: String): Integer;
begin
Result := someMagicAPI_Fuction(PChar(aName));
end;

but I do not know of anything like that

SO

GUESS WHAT

it seems to me the only way to get this type of thing is with an if test or case test for ALL the "String Names" you want to have for your users to use, , and then convert that string name to the constant value for that element type

I think this is standard procedure for having user "words" for programming code types
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11883099
Oh the only alternative I can thing of is to use the string for the numeric value

EName := '$11';

function convertS(aName: String): Integer;
begin
Result := StrToInt(aName);
end;

but I do not think this would be very popular with your users
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 1500 total points
ID: 11883972
you may could use a ComboBox with element Names like "Bold" , "Button" , "Link" or  "Image" and then set the Constant by the index of the Combo Box
0
 

Author Comment

by:Bindza
ID: 11885106
Very good idea, thanks. I wonder why didn't I think of that...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question