Blog di Sara http://blogs.ugidotnet.org/ProgettoDiInstallazione/Default.aspx Il blog di Sara Fabris it-IT Sara Fabris Subtext Version 2.6.0.0 Blog di Sara http://blogs.ugidotnet.org/images/RSS2Image.gif http://blogs.ugidotnet.org/ProgettoDiInstallazione/Default.aspx 77 60 Libreria di Classi C++ - Conversione da String a char http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/05/08/77679.aspx <H2><FONT face=Arial size=2>In questi giorni ho trovato un po' di difficolt&#224; nel trovare documentazione e informazioni sulla conversione di Stringhe in char.<BR>Il problema non&nbsp;&#232; tanto in un progetto WindowsForms o Console (linguaggi C++ .NET) dove l'operazione&nbsp;&#232; piuttosto semplice, ovvero basta scrivere:</FONT></H2> <P><FONT face=Arial size=2>#include &lt;stdlib.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>#include &lt;stdio.h&gt;<BR>#include &lt;<FONT color=#000000>malloc</FONT>.h&gt;<BR><BR><STRONG>void main( void )<BR>{<BR>DWORD len =50;</STRONG></FONT></P> <P><FONT size=2><FONT face=Arial>//ESEGUO L'ALLOCAZIONE DELL'ARRAY DI CHAR DI DIMENSIONE 50<BR><FONT color=#000000><STRONG>char* c1 = (char</STRONG></FONT></FONT><FONT face=Arial color=#000000><STRONG> *) malloc( len ); //</STRONG>oppure basta scrivere char* c1 = new char[50];</FONT></FONT></P> <P><FONT face=Arial color=#000000 size=2><STRONG>String *str = "Hello World";</STRONG></FONT></P> <P><FONT size=2><FONT face=Arial>//INSERISCO I VARI CARATTERI ALL'INTERNO DELL'ARRAY DI CHAR<BR></FONT><FONT face=Arial color=#000000><STRONG>for (int</STRONG></FONT></FONT><FONT face=Arial color=#000000 size=2><STRONG> i=0;i&lt;str-&gt;Length; i++){<BR>&nbsp;&nbsp;&nbsp;&nbsp; c1[i] = Convert::ToChar(str-&gt;Substring(i,1));<BR>}}}</STRONG></FONT></P> <P><FONT face=Arial size=2>Per&#242; se voglio fare lo stesso per un progetto DLL libreria di classi scritto in C++ come faccio?<BR>C'&#232; un esempio di microsoft piuttosto interessante che spiega come risolvere il problema.<BR>L'esempio di trova a questo <A href="http://support.microsoft.com/kb/311259/">http://support.microsoft.com/kb/311259/</A></FONT><BR><FONT face=Arial size=2>In particolare il codice&nbsp;&#232; questo:</FONT></P> <P><FONT face=Arial size=2><STRONG>using namespace System::Runtime::InteropServices;</STRONG></FONT></P> <P><FONT face=Arial size=2><STRONG>int _tmain(void)<BR>{</STRONG><BR>&nbsp;&nbsp; </FONT></P> <P><FONT face=Arial size=2>//Grazie al Metodo Marshal::StringToHGlobalAnsi riesco a convertire una stringa in un'array di char e mi ritorna il puntatore al primo elemento <BR>//dell'array.<BR><STRONG>char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);</STRONG></FONT></P> <P><FONT face=Arial size=2>....eseguo le operazioni su str2</FONT></P> <P><FONT face=Arial size=2>//E' molto importante ricorda che la classe Marshal offre un insieme di metodi per l'allocazione della memoria non gestita. <BR>//</FONT><FONT face=Arial size=2>Quindi quando si lavora con la memoria non gestita non &#232; pi&#249; il Garbage ad occuparsi della deallocazione della memoria, <BR>//ma</FONT><FONT face=Arial size=2>&nbsp;sar&#224; nostro compito (cio&#232; compito del programmatore) occuparsi di ci&#242;. Per la classe Marshal il metodo da richiamare &#232;:<BR>&nbsp;&nbsp;&nbsp;<STRONG>Marshal::FreeHGlobal(str2);</STRONG></FONT></P> <P><FONT face=Arial size=2><STRONG>&nbsp;&nbsp;&nbsp; return 0;<BR>}</STRONG></FONT></P> <P><FONT face=Arial size=2>Spero possa esservi d'aiuto questo blog.<BR>Ciao</FONT></P> <P><FONT face=Arial size=2>Sara</FONT></P><img src="http://blogs.ugidotnet.org/ProgettoDiInstallazione/aggbug/77679.aspx" width="1" height="1" /> Sara Fabris http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/05/08/77679.aspx Tue, 08 May 2007 18:02:00 GMT http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/05/08/77679.aspx#feedback 1143 http://blogs.ugidotnet.org/ProgettoDiInstallazione/comments/commentRss/77679.aspx http://blogs.ugidotnet.org/ProgettoDiInstallazione/services/trackbacks/77679.aspx ImageButton e LinkButton nel Datagrid http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/04/24/76459.aspx <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><STRONG>Come si pu&#242; inserire nel codice di un progetto Asp.net un'ImageButton oppure un linkButton in un Datagrid?? <?xml:namespace prefix = u1 /><u1:p></u1:p><?xml:namespace prefix = o /><o:p></o:p></STRONG></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><STRONG>Nei forum si trovano notizie frammentate e quindi ho deciso di raccogliere tutte qui in questo post. <u1:p></u1:p><o:p></o:p></STRONG></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><STRONG>Vediamo come creare intanto un ImageButton:</STRONG> <BR style="mso-special-character: line-break"><BR style="mso-special-character: line-break"><o:p></o:p></SPAN></P><u1:p></u1:p> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><B><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">public class ImageButtonColumn : TemplateColumn</SPAN></B><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB; mso-bidi-font-weight: bold"> <BR></SPAN><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">{ <BR>private ImageButtonItem imgItem; <u1:p></u1:p><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">public event ImageClickEventHandler Click <BR>{ <BR>add <BR>{ <BR>imgItem.Click += value; <BR>} <BR>remove <BR>{ <BR>imgItem.Click -= value; <BR>}} <u1:p></u1:p><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB"><u1:p>&nbsp;</u1:p>public bool AutoPostBack <BR>{ <BR>set <BR>{ <BR>imgItem.AutoPostBack = value; <BR>} <BR>get <BR>{ <BR>return imgItem.AutoPostBack; <BR>}} <u1:p></u1:p><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">public ImageButtonColumn() <BR>{ <BR>imgItem = new ImageButtonItem(false); <BR>imgItem.AutoPostBack = false; <BR>this.ItemTemplate = imgItem as ITemplate; <BR>}} <u1:p></u1:p><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB"><u1:p>&nbsp;</u1:p><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><B><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">internal class ImageButtonItem : ITemplate</SPAN></B><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB; mso-bidi-font-weight: bold"> <BR></SPAN><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">{ <u1:p></u1:p><BR>private bool readOnly = true; <BR>public event ImageClickEventHandler Click; <u1:p></u1:p><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">public ImageButtonItem(bool editable)&nbsp;<BR>{&nbsp;<BR>readOnly = (editable==true)?false:true; <BR>} <u1:p></u1:p><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB"><u1:p>void ITemplate.InstantiateIn(Control container) <BR>{ <BR>ImageButton ib = new ImageButton(); <BR>ib.ImageUrl = "info.gif";&nbsp; <BR></SPAN><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">//Immagine da Associare al Controllo <BR></SPAN><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">ib.Click += new ImageClickEventHandler(this.OnClick); <BR>container.Controls.Add(ib); <BR>} <u1:p></u1:p><o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB">private void OnClick(object sender, ImageClickEventArgs e) <BR>{ <BR>Click(sender, e); <BR>} <BR>private bool autoPostBack=false; <BR><BR>public bool AutoPostBack <BR>{ <BR>set <BR>{ <BR>autoPostBack = value; <BR>} <BR>get <BR>{ <BR>return autoPostBack; <BR>}}} </SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN lang=EN-GB style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-GB"><o:p></o:p></SPAN>&nbsp;</P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><STRONG><FONT size=2>e ora vediamo la definizione della classe LinkButton:</FONT></STRONG></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><o:p></o:p></SPAN>&nbsp;</P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><B><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial">internal class LinkButtonItem: ITemplate</SPAN></B><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial"> <BR>{ <BR>private bool readOnly = true; <BR>public event EventHandler Click; <BR><BR>public LinkButtonItem(bool editable) { <BR>readOnly = (editable==true)?false:true; <BR>} <BR><BR>void ITemplate.InstantiateIn(Control container) { <BR>LinkButton lb = new LinkButton(); <BR>lb.DataBinding += new EventHandler(this.BindLabelColumn); <BR>lb.Click += new EventHandler(this.OnClick); <BR>container.Controls.Add(lb); <BR>} <BR><BR>public void BindLabelColumn(object sender, EventArgs e) { <BR>LinkButton lb = (LinkButton)sender; <BR>DataGridItem container =(DataGridItem)lb.NamingContainer;<BR>lb.Text = Convert.ToString(System.Web.UI.DataBinder.Eval(container, "DataItem.ID")); <BR></SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: Arial">//CAMPO ASSOCIATO AL DATAGRID - ESTRAZIONE DEL CAMPO ID </SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial"><BR>} <BR><BR>private void OnClick(object sender, EventArgs e) { <BR>Click(sender, e); <BR>} <BR><BR>private bool autoPostBack=true; <BR>public bool AutoPostBack { <BR>set <BR>{ <BR>autoPostBack = value; <BR>} <BR>get<BR>{ <BR>return autoPostBack; <BR>} } } <BR><BR><B>public class LinkButtonColumn : TemplateColumn</B> <BR>{ <BR>private LinkButtonItem imgItem; <BR>public string idRecord; <BR><BR>public event EventHandler Click { <BR>add { <BR>imgItem.Click += value; <BR>} <BR>remove { <BR>imgItem.Click -= value; <BR>} } <BR><BR>public bool AutoPostBack { <BR>set <BR>{ <BR>imgItem.AutoPostBack = value; <BR>} <BR>get <BR>{ <BR>return imgItem.AutoPostBack; <BR>} } <BR><BR>public LinkButtonColumn() <BR>{ <BR>imgItem = new LinkButtonItem(false); <BR>imgItem.AutoPostBack = false; <BR>this.ItemTemplate = imgItem as ITemplate; <BR>}<BR>}</SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial"><o:p></o:p></SPAN>&nbsp;</P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><STRONG>ora prima di eseguire il Bind del Datagrid sar&#224; necessario l'ImageButton o il LinkButton<o:p></o:p></STRONG></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"><o:p>&nbsp;</o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: Arial">//Supponiamo che il nostro DataTable si componga di pi&#249; colonne. La colonna "ID" per&#242; la visualizzo<BR>//come campo LinkButton e la colonna "Colore" la visualizzo come ImageButton<BR><BR></SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial">foreach (DataColumn dc in dt.Columns) { <BR><BR>if (dc.ColumnName.ToLower() == "id") {<BR><BR>LinkButtonColumn linkcol = new LinkButtonColumn();&nbsp; </SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: Arial">// LINKBUTTON NELLA COLONNA ID</SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial"><BR>linkcol .HeaderText= dc.ColumnName; <BR>linkcol .Click +=new EventHandler(linkcol_Click);&nbsp; </SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: Arial">//ASSOCIO L'EVENTO CLICK</SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial"><BR>MyDataGrid.Columns.Add(linkcol); <BR>} <BR><BR>else if (dc.ColumnName.ToLower() == "colore") { <BR><BR>ImageButtonColumn imgcol = new ImageButtonColumn();&nbsp; //</SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: Arial">IMAGEBUTTON NELLA COLONNA COLORE</SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial"><BR>imgcol.HeaderText= dc.ColumnName; <BR>imgcol.Click +=new EventHandler(imgcol_Click);&nbsp;&nbsp; //ASSOCIO L'EVENTO CLICK<BR>MyDataGrid.Columns.Add(imgcol); <BR><BR>} <BR>else { <BR>BoundColumn b=new BoundColumn(); <BR>b.DataField=dc.ColumnName; <BR>b.DataFormatString="{0}"; <BR>b.HeaderText=dc.ColumnName; <BR>MyDataGrid.Columns.Add(b); <BR>} } <BR><BR>MyDataGrid.DataSource = dt; <BR>MyDataGrid.DataBind();<o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: Arial">//I DUE EVENTI SARANNO DEFINITI COME SEGUE:<o:p></o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial">private void imgcol_Click(object sender, ImageClickEventArgs e) {&nbsp;<BR>//CODICE<BR>} <BR><BR>private void linkcol_Click(object sender, EventArgs e) { <BR><BR>LinkButton lbtn = (LinkButton)sender; <BR>DataGridItem dgi = (DataGridItem)lbtn.NamingContainer;&nbsp; //estraggo la colonna del datagrid<BR>//....<BR><BR>}</SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial"></SPAN>&nbsp;</P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 9pt; COLOR: navy; FONT-FAMILY: Arial"><o:p></o:p></SPAN>&nbsp;</P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><STRONG><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">Ciao a tutti e spero possa essere&nbsp; </SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">d'aiuto il post.</SPAN></STRONG><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: Arial"><o:p></o:p></SPAN></P></u1:p><img src="http://blogs.ugidotnet.org/ProgettoDiInstallazione/aggbug/76459.aspx" width="1" height="1" /> Sara Fabris http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/04/24/76459.aspx Tue, 24 Apr 2007 18:17:00 GMT http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/04/24/76459.aspx#feedback 28 http://blogs.ugidotnet.org/ProgettoDiInstallazione/comments/commentRss/76459.aspx http://blogs.ugidotnet.org/ProgettoDiInstallazione/services/trackbacks/76459.aspx Classe per leggere e scrivere da Registro http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/02/27/71819.aspx <FONT color=#0000ff><FONT color=#0000ff> <P><EM><FONT face=Arial color=#000080 size=2>using</FONT></EM></FONT><EM><FONT face=Arial color=#000080 size=2> namespace</FONT></EM><EM><FONT face=Arial color=#000080 size=2> Microsoft::Win32;<BR>using namespace System;</FONT></EM></P> <P><FONT face=Arial color=#000000 size=2><STRONG>La classe seguente permette di leggere e scrivere nel Registro di Sistema<BR>(classe in C++.net - Framework 1.1)</STRONG></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM>public</EM></FONT></FONT><FONT face=Arial color=#000080 size=2><EM> __gc class</EM></FONT><FONT face=Arial><FONT size=2><FONT color=#000080><EM> Registro<BR>{<BR>private</EM></FONT><FONT color=#000080><EM>:</EM></FONT></FONT></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM>String *CU; </EM></FONT><FONT face=Arial color=#000080 size=2><EM>//= S"CU"; //current user<BR>String *LM; </EM></FONT><FONT face=Arial color=#000080 size=2><EM>// = S"LM"; //Local_Machine<BR>String *CR; </EM></FONT><FONT face=Arial color=#000080 size=2><EM>// = S"CR"; //classes_root<BR>String *UR; </EM></FONT><FONT color=#000080><EM><FONT face=Arial size=2>// = S"UR"; // Users<BR>String *CC; // = S"CC"; // current_config</FONT></P></EM></FONT> <P><FONT face=Arial color=#000080 size=2><EM>public</EM></FONT><FONT face=Arial color=#000080 size=2><EM>:<BR>Registro(){<BR>CU = S"CU"; <BR>LM = S"LM";<BR>CR = S"CR";<BR>UR = S"UR";<BR>CC = S"CC";<BR>};</EM></FONT></P> <P><FONT face=Arial><FONT size=2><FONT color=#000080><EM>String* GetValue(String* key, String* subKey, String* entry);<BR></EM></FONT><FONT color=#000080><EM>int</EM></FONT></FONT></FONT><FONT face=Arial color=#000080 size=2><EM> CreateKey(String* key, String* subKey);<BR>int</EM></FONT><FONT face=Arial color=#000080 size=2><EM> setValue(String* key, String* subKey, String* item, Object* _value);<BR>int</EM></FONT><FONT face=Arial color=#000080 size=2><EM> deleteValue(String* key, String *subKey, String *entry);<BR>};</EM></FONT></P><FONT color=#000080> <P><BR></FONT><FONT face=Arial color=#000000 size=2><STRONG>Il seguente metodo permette di ottenere un valore da una chiave di Registro.<BR>Il primo parametro deve essere CU,LM,CR,UR oppure CC.<BR>Il secondo parametro &#232; il nodo di Registro ad es. "Software\Microsoft\Microsoft Sql Server"<BR>Il terzo parametro &#232; la stringa di Registro dal quale ottenere il valore<BR>In caso di errore ritorna il valore&nbsp;null altrimenti ritorna il valore della chiave di Registro.</STRONG></FONT><FONT color=#000080><BR><EM><FONT face=Arial size=2>String* Registro::GetValue(String* key, String* subKey, String* entry){<BR>RegistryKey *reg;<BR></FONT></EM></FONT><FONT face=Arial color=#000080 size=2><EM>try</EM></FONT><FONT face=Arial color=#000080 size=2><EM>{<BR>if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"CU")<BR></EM></FONT><FONT face=Arial color=#000080 size=2><EM>&nbsp; reg = Registry::CurrentUser-&gt;OpenSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"LM")<BR>&nbsp; reg = Registry::LocalMachine-&gt;OpenSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"UR")<BR>&nbsp; reg = Registry::Users-&gt;OpenSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == "CC")<BR>&nbsp; reg = Registry::CurrentConfig-&gt;OpenSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == "CR")<BR>&nbsp; reg = Registry::Users-&gt;OpenSubKey(subKey);<BR></EM></FONT><FONT face=Arial color=#000080 size=2><EM>else<BR>&nbsp; </EM></FONT><FONT face=Arial color=#000080 size=2><EM>reg = Registry::LocalMachine-&gt;OpenSubKey(subKey);<BR><BR>return</EM></FONT><FONT face=Arial color=#000080 size=2><EM> reg-&gt;GetValue(entry, 0)-&gt;ToString();<BR>}<BR>catch</EM></FONT><FONT face=Arial color=#000080 size=2><EM>(Exception* ){<BR>return</EM></FONT><FONT face=Arial color=#000080 size=2><EM> 0;<BR>}};</EM></FONT></P> <P><FONT face=Arial size=2></FONT>&nbsp;</P> <P><STRONG><FONT face=Arial size=2>Il seguente metodo permette di cancellare una chiave di Registro.<BR>Il primo parametro deve essere CU,LM,CR,UR oppure CC.<BR>Il secondo parametro &#232; il nodo di Registro ad es. "Software\Microsoft\Microsoft Sql Server"<BR>Il terzo parametro &#232; la stringa di Registro da cancellare<BR>In caso di errore ritorna il valore 1, altrimenti valore 0.</FONT></STRONG><STRONG><BR></STRONG><FONT face=Arial color=#000080 size=2><EM>int</EM></FONT><FONT face=Arial color=#000080 size=2><EM> Registro::deleteValue(String *key, String *subKey, String *entry){<BR></EM></FONT><FONT face=Arial color=#000080 size=2><EM>RegistryKey *reg;<BR></EM></FONT><FONT face=Arial><FONT size=2><FONT color=#000080><EM>try<BR></EM></FONT><FONT color=#000080><EM>{</EM></FONT></FONT></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM>if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"CU")<BR>&nbsp; reg = Registry::CurrentUser-&gt;CreateSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"LM")<BR>&nbsp; reg = Registry::LocalMachine-&gt;CreateSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"UR")<BR>&nbsp; </EM></FONT><FONT face=Arial color=#000080 size=2><EM>reg = Registry::Users-&gt;CreateSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == "CC")<BR>&nbsp; reg = Registry::CurrentConfig-&gt;CreateSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == "CR")<BR>&nbsp; reg = Registry::Users-&gt;CreateSubKey(subKey);<BR></EM></FONT><FONT face=Arial color=#000080 size=2><EM>else<BR>&nbsp; </EM></FONT><FONT face=Arial color=#000080 size=2><EM>reg = Registry::Users-&gt;CreateSubKey(subKey);<BR><BR>reg-&gt;DeleteValue(entry);<BR>return</EM></FONT><FONT face=Arial color=#000080 size=2><EM> 0;<BR>}<BR>catch</EM></FONT><FONT face=Arial color=#000080 size=2><EM>(Exception* ){<BR>return</EM></FONT><FONT face=Arial color=#000080 size=2><EM> 1;<BR>}};</EM></FONT></P> <P><FONT color=#000080><BR><FONT face=Arial><FONT size=2><STRONG><FONT color=#000000>Il seguente metodo permette di creare un nodo di Registro.<BR>Il primo parametro deve essere CU,LM,CR,UR oppure CC.<BR>Il secondo parametro &#232; il nodo di&nbsp;Registro da creare&nbsp;ad es. "Software\Microsoft\Microsoft Sql Server\Test"<BR>In caso di errore ritorna il valore 1, altrimenti valore 0.<BR></FONT></STRONG><EM>int</EM></FONT></FONT></FONT><FONT face=Arial color=#000080 size=2><EM> Registro::CreateKey(String* key, String* subKey){</EM></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM>RegistryKey* reg;<BR></EM></FONT><FONT face=Arial><FONT size=2><FONT color=#000080><EM>try<BR></EM></FONT><FONT color=#000080><EM>{</EM></FONT></FONT></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM>if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"CU")<BR>&nbsp; reg = Registry::CurrentUser-&gt;CreateSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"LM")<BR>&nbsp; reg = Registry::LocalMachine-&gt;CreateSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"UR")<BR>&nbsp; reg = Registry::Users-&gt;CreateSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == "CC")<BR>&nbsp; reg = Registry::CurrentConfig-&gt;CreateSubKey(subKey);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == "CR")<BR>&nbsp; reg = Registry::Users-&gt;CreateSubKey(subKey);<BR></EM></FONT><FONT face=Arial color=#000080 size=2><EM>else<BR>&nbsp; </EM></FONT><FONT face=Arial><FONT size=2><FONT color=#000080><EM>reg = Registry::Users-&gt;CreateSubKey(subKey);<BR></EM></FONT><BR><FONT color=#000080><EM>return</EM></FONT></FONT></FONT><FONT face=Arial color=#000080 size=2><EM> 0;<BR>}<BR>catch</EM></FONT><FONT face=Arial color=#000080 size=2><EM>(Exception* )<BR>{<BR>return</EM></FONT><FONT face=Arial color=#000080 size=2><EM> 1;<BR>}};</EM></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM></EM></FONT>&nbsp;</P> <P><FONT face=Arial><FONT size=2><FONT color=#000080><STRONG><FONT color=#000000>Il seguente metodo permette di creare una chiave di Registro e di assegnargli un valore.<BR>Il primo parametro deve essere CU,LM,CR,UR oppure CC.<BR>Il secondo parametro &#232;&nbsp;il nodo di Registro gi&#224; presente ad es. "Software\Microsoft\Microsoft Sql Server"<BR>Il terzo parametro &#232; la chiave di Registro da creare e l'ultimo parametro &#232; il valore che vogliamo assegnargli.<BR>In caso di errore ritorna il valore 1, altrimenti valore 0.</FONT></STRONG><BR><EM>int</EM></FONT><FONT color=#000080><EM> Registro::setValue(String* key, String* subKey, String* item, Object* _value){</EM></FONT></FONT></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM>RegistryKey* reg;<BR></EM></FONT><FONT face=Arial><FONT size=2><FONT color=#000080><EM>try<BR></EM></FONT><FONT color=#000080><EM>{</EM></FONT></FONT></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM>if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"CU")<BR>&nbsp; reg = Registry::CurrentUser-&gt;OpenSubKey(subKey, true</EM></FONT><FONT face=Arial color=#000080 size=2><EM>);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"LM")<BR>&nbsp; reg = Registry::LocalMachine-&gt;OpenSubKey(subKey, true</EM></FONT><FONT face=Arial color=#000080 size=2><EM>);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == S"UR")<BR>&nbsp; reg = Registry::Users-&gt;OpenSubKey(subKey, true</EM></FONT><FONT face=Arial color=#000080 size=2><EM>);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == "CC")<BR>&nbsp; reg = Registry::CurrentConfig-&gt;OpenSubKey(subKey, true</EM></FONT><FONT face=Arial color=#000080 size=2><EM>);<BR>else if</EM></FONT><FONT face=Arial color=#000080 size=2><EM> (key == "CR")<BR>&nbsp; reg = Registry::Users-&gt;OpenSubKey(subKey, true</EM></FONT><FONT face=Arial color=#000080 size=2><EM>);<BR></EM></FONT><FONT face=Arial color=#000080 size=2><EM>else<BR>&nbsp; reg = Registry::Users-&gt;OpenSubKey(subKey, true</EM></FONT><FONT face=Arial color=#000080 size=2><EM>);<BR><BR>reg-&gt;SetValue(item, _value);<BR>reg-&gt;Close();<BR>return</EM></FONT><FONT face=Arial><FONT size=2><FONT color=#000080><EM> 0;<BR>}</EM><BR></FONT><FONT color=#0000ff>catch</FONT>(Exception* )<BR>{<BR><FONT color=#0000ff>return</FONT> 1;<BR>}};</FONT></FONT></P><img src="http://blogs.ugidotnet.org/ProgettoDiInstallazione/aggbug/71819.aspx" width="1" height="1" /> Sara Fabris http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/02/27/71819.aspx Tue, 27 Feb 2007 15:36:00 GMT http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2007/02/27/71819.aspx#feedback 553 http://blogs.ugidotnet.org/ProgettoDiInstallazione/comments/commentRss/71819.aspx http://blogs.ugidotnet.org/ProgettoDiInstallazione/services/trackbacks/71819.aspx Apertura PDF su una determinata pagina http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/08/04/24719.aspx <P><FONT face=Arial size=2>In questo post vediamo come poter aprire un PDF da un'applicazione e come poter aprire il file specificando la pagina desiderata.<BR>Se si apre una command line e si prova ad aprire un file direttamente da qui alla pagina desiderata baster&#224; scrivere:<BR><STRONG>C:\Programmi\Adobe\..\acrobat.exe /A page=2 C:\abc.pdf</STRONG><BR>Ovvero con l'opzione "/A" si specificano i parametri con cui aprire il pdf e di seguito si indica il path dello stesso file da aprire.<BR>Per maggiori informazioni sui parametri da specificare basta guardare <A href="http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf">qui</A>. </FONT></P> <P><FONT face=Arial size=2>In questo caso per&#242; nella mia macchina &#232; installato Adobe Acrobat, se invece &#232; installato solamente l'Acrobat Reader allora da command line si dovr&#224; scrivere:<BR><STRONG>C:\Programmi\Adobe\..\acrord32.exe /A page = 2 C:\abc.pdf</STRONG></FONT></P> <P><FONT face=Arial size=2>A questo punto dobbiamo fare lo stesso da codice!<BR>Dobbiamo recuperare il path di Installazione di Adobe, prestando attenzione alla versione e a quale Adobe &#232; stato installato.<BR></FONT><FONT color=#0000ff><FONT face=Arial color=#000000 size=2>(Codice VB.NET)</FONT><BR></FONT><FONT color=#0000ff><BR></FONT><FONT color=#000080><FONT face=Arial size=2>Dim</FONT><FONT face=Arial size=2> buf As </FONT></FONT><FONT face=Arial><FONT color=#000080 size=2>String = "C:\abc.pdf" <FONT color=#000000><EM>'Path del file PDF da aprire</EM></FONT><BR>Dim pathKey As </FONT></FONT><FONT color=#000080><FONT face=Arial size=2>String<BR></FONT><FONT face=Arial><FONT size=2>Dim key As RegistryKey</FONT></FONT></FONT></P> <P><FONT color=#000080><FONT face=Arial color=#000000 size=2><EM>'Setto il percorso di Adobe nel Registro di Sistema<BR>'Controllo prima la presenza di Acrobat Reader e in seguito quella di Adobe Acrobat standard.</EM></FONT><BR></FONT><FONT color=#000080><FONT face=Arial size=2>pathKey = "Software\Adobe\Acrobat Reader\"<BR>key = Registry.LocalMachine.OpenSubKey(pathKey)<BR></FONT><BR><FONT face=Arial size=2>If</FONT><FONT face=Arial size=2> (key Is Nothing) </FONT></FONT><FONT face=Arial color=#000080 size=2>Then<BR>&nbsp; </FONT><FONT face=Arial><FONT size=2><FONT color=#000080>pathKey = "Software\Adobe\Adobe Acrobat\"<BR>&nbsp; key = Registry.LocalMachine.OpenSubKey(pathKey)<BR>End If</FONT><FONT color=#0000ff><FONT color=#0000ff></P></FONT></FONT></FONT> <P><FONT face=Arial><FONT color=#000080 size=2>If (key Is Nothing) </FONT></FONT><FONT face=Arial color=#000080 size=2>Then<BR>&nbsp;</FONT><FONT face=Arial color=#000000 size=2><EM>'Non sono state trovate n&#232; le chiavi di Acrobat Reader n&#232; quelle di Adobe Acrobat<BR>&nbsp;</EM></FONT><FONT face=Arial><FONT color=#000080 size=2><EM><FONT color=#000000>'Viene aperto il pdf sulla prima pagina!</FONT><BR></EM>&nbsp;ApriFile(buf, Nothing</FONT></FONT><FONT face=Arial color=#000080 size=2>)<BR>&nbsp;</FONT><FONT color=#000080><FONT face=Arial><FONT size=2>Return<BR>End </FONT></FONT><FONT face=Arial size=2>If</FONT></FONT></P> <P><FONT face=Arial color=#000000 size=2><EM>'Per trovare dove &#232; installato l'Adobe dobbiamo&nbsp;trovare la chiave di registro corretta che solitamente si trova in:<BR>' Software\Adobe\Adobe Acrobat\7.0\InstallPath oppure<BR>' Software\Adobe\Acrobat Reader\5.0\InstallPath<BR>'Quello che &#232; importante trovare &#232; la versione di Adobe&nbsp;installata. Se ci sono pi&#249; versioni installate, <BR>'viene presa l'ultima versione presente</EM>.</FONT><FONT color=#0000ff></P><FONT face=Arial size=2></FONT><FONT color=#0000ff> <P></FONT><FONT color=#000080><FONT face=Arial size=2>Dim</FONT><FONT face=Arial><FONT size=2> values As String</FONT></FONT></FONT><FONT size=2><FONT color=#000080><FONT face=Arial>() = key.GetSubKeyNames()<BR>Dim maxVersion As String</FONT><FONT face=Arial> = "0"</FONT></FONT></FONT></P> <P><FONT face=Arial><FONT color=#000080 size=2>For Each s As String In</FONT></FONT><FONT face=Arial color=#000080 size=2> values<BR>&nbsp;</FONT><FONT face=Arial><FONT color=#000080 size=2>Try<BR>&nbsp;&nbsp; If (Double.Parse(s) &gt; Double.Parse(maxVersion)) </FONT></FONT><FONT face=Arial color=#000080 size=2>Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face=Arial color=#000080 size=2>maxVersion = s<BR>&nbsp;&nbsp; End </FONT><FONT face=Arial><FONT color=#000080 size=2>If<BR>&nbsp;Catch ex As</FONT></FONT><FONT face=Arial color=#000080 size=2> Exception<BR>&nbsp;End </FONT><FONT face=Arial color=#000080 size=2>Try<BR></FONT><FONT face=Arial><FONT color=#000080 size=2>Next<BR><BR>If (maxVersion = "0") </FONT></FONT><FONT face=Arial><FONT size=2><FONT color=#000080>Then<BR></FONT><FONT color=#000000>&nbsp; <EM>'Se non &#232; stata trovata una versione di Adobe installata, il pdf viene aperto sulla prima pagina</EM></FONT></FONT><BR><FONT face=Arial size=2>&nbsp;<FONT color=#000080> </FONT></FONT></FONT><FONT face=Arial><FONT color=#000080 size=2>ApriFile(buf, Nothing</FONT></FONT><FONT face=Arial color=#000080 size=2>)<BR></FONT><FONT face=Arial color=#000080 size=2>Else<BR>&nbsp;&nbsp; <FONT color=#000000><EM>'Il percorso nel registro &#232; stato costruito.&nbsp;Estraiamo il percorso di installazione di Adobe che solitamente &#232;:<BR>&nbsp;&nbsp;&nbsp;' C:\Programmi\Adobe\Acrobat 7.0\Acrobat&nbsp;oppure C:\Programmi\Adobe\Acrobat 7.0\Reader<BR></EM></FONT></FONT><FONT face=Arial color=#000080 size=2><BR>&nbsp;&nbsp; </FONT><FONT face=Arial color=#000000 size=2><FONT color=#000080>pathKey = pathKey &amp; maxVersion &amp; "\InstallPath\"<BR>&nbsp;&nbsp; key = Registry.LocalMachine.OpenSubKey(pathKey)<BR></FONT><BR>&nbsp;&nbsp; <EM>'La variabile pathAdobe dovr&#224; contenere il percorso di installazione di Adobe.E' necessario controllare su &#232; presente Acrobat Reader <BR>&nbsp;&nbsp; '(l'eseguibile &#232; acrord32.exe) oppure Adobe Acrobat (l'eseguibile &#232; acrobat.exe)</EM></FONT></P> <P><FONT face=Arial color=#000000 size=2>&nbsp;&nbsp; <FONT color=#000080>Dim pathAdobe As </FONT></FONT><FONT face=Arial><FONT color=#000080 size=2>String<BR>&nbsp;&nbsp; pathAdobe = key.GetValue("") &amp; "\acrord32.exe"</FONT></FONT></P> <P><FONT face=Arial color=#000080 size=2>&nbsp;&nbsp; If (New FileInfo(pathAdobe).Exists = False) </FONT><FONT face=Arial color=#000080 size=2>Then&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face=Arial color=#000080 size=2>pathAdobe = key.GetValue("") &amp; "\acrobat.exe"<BR>&nbsp;&nbsp;&nbsp;&nbsp; If (New FileInfo(pathAdobe).Exists = False) </FONT><FONT face=Arial><FONT color=#000080 size=2>Then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ApriFile(buf, Nothing</FONT></FONT><FONT face=Arial color=#000080 size=2>)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </FONT><FONT face=Arial><FONT color=#000080 size=2>Return<BR>&nbsp;&nbsp;&nbsp; &nbsp;End </FONT></FONT><FONT face=Arial><FONT color=#000080 size=2>If<BR>&nbsp;&nbsp; End </FONT></FONT><FONT face=Arial color=#000080 size=2>If<BR><BR></FONT><FONT color=#000080><FONT face=Arial size=2>&nbsp;&nbsp; ApriFile(pathAdobe, "/A page=" &amp; pagina &amp; " " &amp; buf)<BR>End </FONT><FONT face=Arial size=2>If</FONT></FONT></P><FONT color=#0000ff> <P><FONT face=Arial color=#000000 size=2><STRONG>Infine il metodo per lanciare l'apertura del pdf:</STRONG></FONT></P><FONT color=#0000ff> <P></FONT><FONT face=Arial color=#000080 size=2>Private</FONT><FONT size=2><FONT face=Arial color=#000080> Sub ApriFile(ByVal pathFile As String, ByVal argomenti As String</FONT><FONT face=Arial color=#000080>)<BR></FONT></FONT><FONT size=2><FONT face=Arial color=#000080>Dim info As System.Diagnostics.ProcessStartInfo = New</FONT><FONT face=Arial color=#000080> System.Diagnostics.ProcessStartInfo(pathFile)<BR></FONT></FONT><FONT size=2><FONT face=Arial color=#000080>info.UseShellExecute = </FONT><FONT face=Arial color=#000080>True<BR></FONT></FONT><FONT face=Arial color=#000080 size=2>info.Verb = "Open"</FONT></P> <P><FONT face=Arial><FONT color=#000080 size=2>If (argomenti &lt;&gt; Nothing And argomenti &lt;&gt; "") Then<BR>&nbsp;&nbsp; </FONT></FONT><FONT color=#000080><FONT face=Arial size=2>info.Arguments = argomenti<BR></FONT><FONT face=Arial><FONT size=2>End If</P></FONT></FONT></FONT> <P><FONT color=#000080><FONT face=Arial size=2>System.Diagnostics.Process.Start(info)<BR></FONT><FONT face=Arial><FONT size=2>End </FONT></FONT><FONT face=Arial size=2>Sub</FONT></FONT></P> <P><FONT color=#000080 size=2></FONT>&nbsp;</P> <P></FONT></FONT><FONT color=#000000 size=2><STRONG><U>P.S:</U></STRONG> <U>Questo post l'ho potuto scrivere anche grazie all'aiuto di Raffaele Rialdi che mi ha aiutato nella risoluzione del problema nel forum di microsoft </U>(<A href="http://support.microsoft.com/newsgroups/default.aspx">qui</A>). <U>Quindi un doveroso grazie va anche a lui!!!</U></FONT></FONT></P><img src="http://blogs.ugidotnet.org/ProgettoDiInstallazione/aggbug/24719.aspx" width="1" height="1" /> Sara Fabris http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/08/04/24719.aspx Thu, 04 Aug 2005 15:18:00 GMT http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/08/04/24719.aspx#feedback 2326 http://blogs.ugidotnet.org/ProgettoDiInstallazione/comments/commentRss/24719.aspx http://blogs.ugidotnet.org/ProgettoDiInstallazione/services/trackbacks/24719.aspx Richieste Http e Proxy http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/06/27/21779.aspx <P><FONT face=Arial size=2>In questo post vediamo come poter scrivere del codice C# per effettuare richieste Http da un'applicazione.<BR>I problemi che si possono incontrare in queste richieste Http possono essere soprattutto relativi al Proxy, in quanto non </FONT><FONT face=Arial size=2>permette all'applicazione di effettuare richieste internet.</FONT></P> <P><FONT face=Arial size=2>Per prima cosa vediamo quali sono le classi che verranno utilizzate:</FONT></P> <P><FONT face=Arial color=#000080 size=2>using System.Net.Sockets;<BR>using System.Net;<BR>using System.IO;<BR>using System.Text;</FONT></P> <P><FONT face=Arial size=2>Nel mio progetto ho inserito un oggetto Button e quando questo viene premuto viene richiamato l'evento click che effettua la </FONT><FONT face=Arial size=2>richiesta Http. Questo &#232; il codice:</FONT></P> <P><FONT face=Arial size=2><FONT color=#000080>private void button2_Click(object sender, System.EventArgs e)<BR>{<BR>string valVerifica= "";<BR>try<BR>{<BR></FONT><EM><STRONG><FONT color=#000080>//Indirizzo dell'url Http o Https</FONT></STRONG></EM> <BR><FONT color=#000080>string urlRegistro = @http://www......;</FONT></FONT></P> <P><FONT face=Arial size=2><FONT color=#000080>HttpWebRequest wrInfoCam =(HttpWebRequest) HttpWebRequest.Create(urlRegistro);<BR>wrInfoCam.KeepAlive = false;</FONT> <BR><FONT color=#000080><EM><STRONG>//La propriet&#224; KeepAlive a false permette di NON impostare connessione persistenti ai server che<BR>//le supportano.</STRONG></EM></FONT></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM><STRONG>//IMPOSTAZIONI PROXY.<BR>//questa parte di codice pu&#242; essere omessa nel caso non si utilizzi un proxy</STRONG></EM></FONT></P> <P><FONT face=Arial color=#000080 size=2><EM><STRONG>//Definizione credenziali</STRONG></EM><BR>NetworkCredential cred = new NetworkCredential("sara", "xxx");</FONT></P> <P><FONT face=Arial color=#000080 size=2>CredentialCache myCache = new CredentialCache(); <BR>myCache.Add(new Uri("</FONT><A href="http://master.lan.xxxx.it:8080/"><FONT face=Arial color=#000080 size=2>http://master.lan.xxxx.it:8080/</FONT></A><FONT face=Arial color=#000080 size=2>"), "Basic", cred); </FONT></P> <P><FONT face=Arial color=#000080 size=2>WebProxy _proxy = new WebProxy();<BR>_proxy = (WebProxy)(wrInfoCam.Proxy);</FONT></P> <P><FONT face=Arial color=#000080 size=2>Uri _uri = new Uri("</FONT><A href="http://master.lan.xxxx.it:8080/"><FONT face=Arial color=#000080 size=2>http://master.lan.xxxx.it:8080/</FONT></A><FONT face=Arial color=#000080 size=2>");<BR>_proxy.Address = _uri;<BR>_proxy.Credentials = myCache;</FONT></P> <P><FONT face=Arial color=#000080 size=2>wrInfoCam.Proxy = _proxy;<BR><EM><STRONG>//FINE IMPOSTAZIONE PROXY</STRONG></EM></FONT></P> <P><FONT face=Arial color=#000080 size=2>RequestStateHttp reqState = new RequestStateHttp();<BR>reqState.request = wrInfoCam;</FONT></P> <P><FONT face=Arial color=#000080 size=2>reqState.request.Method="POST";<BR>wrInfoCam.ContentType= "application/x-www-form-urlencoded";</FONT></P> <P><FONT face=Arial color=#000080 size=2>IAsyncResult result =(IAsyncResult) (wrInfoCam.BeginGetRequestStream(new AsyncCallback(ReadCallbackHttp), reqState));</FONT></P> <P><FONT face=Arial color=#000080 size=2><EM><STRONG>//Attendo la risposta del sito http o https mediante l'oggetto HttpWebResponse.</STRONG></EM><BR>HttpWebResponse myHttpWebResponse=(HttpWebResponse) (wrInfoCam.GetResponse());</FONT></P> <P><FONT face=Arial color=#000080 size=2>Stream streamResponse=myHttpWebResponse.GetResponseStream();<BR>StreamReader streamRead = new StreamReader(streamResponse);<BR>char [] readBuff = new Char[256];<BR>int count = streamRead.Read(readBuff, 0, 256);</FONT></P> <P><FONT face=Arial color=#000080 size=2>while (count &gt; 0) <BR>{<BR>valVerifica = new String(readBuff, 0, count);<BR>count = streamRead.Read(readBuff, 0, 256);<BR>}<BR>streamResponse.Close();<BR>streamRead.Close();<BR>myHttpWebResponse.Close();</FONT></P> <P><FONT face=Arial color=#000080 size=2>MessageBox.Show(valVerifica);</FONT></P> <P><FONT face=Arial color=#000080 size=2>}<BR>catch(Exception ex)<BR>{<BR>MessageBox.Show(ex.ToString());<BR>}<BR>}</FONT></P> <P><FONT face=Arial size=2>A questo punto manca da definire la nostra classe RequestStateHttp, che consente di passare i dati su chiamate asincrone:</FONT></P> <P><FONT face=Arial color=#000080 size=2>public class RequestStateHttp <BR>{<BR>&nbsp;static int BUFFER_SIZE = 1024;</FONT></P> <P><FONT face=Arial color=#000080 size=2>&nbsp;public StringBuilder requestData;<BR>&nbsp;public Byte [] bufferRead;<BR>&nbsp;public HttpWebRequest&nbsp; request;<BR>&nbsp;public HttpWebResponse&nbsp; response;<BR>&nbsp;public Stream responseStream;</FONT></P> <P><FONT face=Arial color=#000080 size=2>&nbsp;public RequestStateHttp() <BR>&nbsp;{<BR>&nbsp;&nbsp;bufferRead = new Byte[BUFFER_SIZE];<BR>&nbsp;&nbsp;requestData = new StringBuilder("");<BR>&nbsp;&nbsp;request = null;<BR>&nbsp;&nbsp;responseStream = null;<BR>&nbsp;}<BR>}</FONT></P> <P><FONT face=Arial size=2>E rimane da definire il metodo ReadCallbackHttp che consente di implementare il metodo di callback asincrono per la lettura </FONT><FONT face=Arial size=2>del flusso di risposta. I dati ricevuti dalla risorsa Internet vengono trasferiti nella propriet&#224; ResponseData dell'istanza </FONT><FONT face=Arial size=2>di RequestStateHttp, quindi si inizia un'altra lettura asincrona del flusso di risposta finch&#233; non viene pi&#249; restituito alcun </FONT><FONT face=Arial size=2>dato. </FONT></P> <P><FONT face=Arial color=#000080 size=2>void ReadCallbackHttp(IAsyncResult async)<BR>{<BR>&nbsp;try<BR>&nbsp;{<BR>&nbsp;&nbsp;RequestStateHttp myRequestState=(RequestStateHttp)(async.AsyncState);<BR>&nbsp;&nbsp;HttpWebRequest myHttpWebRequest2=(HttpWebRequest)(myRequestState.request);<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;Stream postStream=myHttpWebRequest2.EndGetRequestStream(async);</FONT></P> <P><FONT face=Arial color=#000080 size=2>&nbsp;&nbsp;ASCIIEncoding encoder = new ASCIIEncoding();</FONT></P> <P><FONT face=Arial color=#000080 size=2>&nbsp;&nbsp;string parametriUrl = "...eventuali parametri da passare all'url...";<BR>&nbsp;<STRONG>&nbsp;//se ci sono pi&#249; parametri richiesti da passare all'url allora &#232; necessario utilizzare l'&amp;, ovvero:<BR>&nbsp;&nbsp;//"USer=xx&amp;Password=yy"</STRONG></FONT></P> <P><FONT face=Arial color=#000080 size=2>&nbsp;&nbsp;Byte [] byteArray = encoder.GetBytes(parametriUrl);</FONT></P> <P><FONT face=Arial color=#000080 size=2>&nbsp;&nbsp;postStream.Write(byteArray,0,parametriUrl.Length);<BR>&nbsp;&nbsp;postStream.Close();<BR>&nbsp;}<BR>&nbsp;catch (Exception ex)<BR>&nbsp;{<BR>&nbsp;&nbsp;MessageBox.Show(ex.ToString());<BR>&nbsp;}<BR>}</FONT></P> <P><FONT face=Arial size=2>Tutto il codice qui inserito &#232; testato e funzionante.<BR>Se avete un proxy impostato sulla vostra rete e durante l'esecuzione del codice nel metodo ReadCallBackHttp, nel metodo EndGetRequestStream si genera un'eccezione del tipo "Connessione sottostate chiusa. Errore 407: Autenticazione Proxy mancante" ci possono essere due tipo di errori principalmente:<BR>1. i dati relativi all'indirizzo del proxy o le credenziali sono errate. Per es. l'indirizzo del proxy deve essere scritto come: </FONT><FONT face=Arial size=2>http://master.lan.xxxx.it</FONT><FONT face=Arial size=2>, mentre l'uri dev'essere nel formato: </FONT><FONT face=Arial size=2>http://master.lan.xxxx.it:PORTA/</FONT><FONT face=Arial size=2>.</FONT></P> <P><FONT face=Arial size=2>2. Il proxy utilizza due tipi di autenticazione, quella Basic e quella NTLM ovvero autenticazione windows. Ho potuto notare che la sola autenticazione Basic settata, la richiesta all'indirizzo http/https funziona correttamente, mentre se &#232; settata anche l'autenticazione NTLM ci possono essere dei problemi con le credenziali, che possono non venire riconosciute.</FONT></P> <P>&nbsp;</P> <P><FONT face=Arial><FONT size=2><STRONG><U>P.S:</U></STRONG> <U>Questo post l'ho potuto scrivere anche grazie all'aiuto di Gabriele Gaggi che mi ha aiutato nella risoluzione del problema. Quindi un doveroso grazie va anche a lui!!!</U></FONT></FONT></P><img src="http://blogs.ugidotnet.org/ProgettoDiInstallazione/aggbug/21779.aspx" width="1" height="1" /> Sara Fabris http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/06/27/21779.aspx Mon, 27 Jun 2005 11:22:00 GMT http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/06/27/21779.aspx#feedback 1754 http://blogs.ugidotnet.org/ProgettoDiInstallazione/comments/commentRss/21779.aspx http://blogs.ugidotnet.org/ProgettoDiInstallazione/services/trackbacks/21779.aspx File Resources http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/02/07/10414.aspx <P><FONT face=Arial color=#000000 size=2>Per poter creare un file resources dove inserire Icone o Immagini da poter utilizzare all'interno del vostro applicativo baster&#224; scrivere:</FONT><BR><FONT color=#000080><FONT face=Arial size=2>Icon ic = new </FONT><FONT face=Arial size=2>Icon(@"C:\myIco.ico"</FONT></FONT><FONT color=#000080><FONT face=Arial size=2>;); <BR>IResourceWriter rw = new </FONT><FONT face=Arial size=2>ResourceWriter(@"C:\Image.resources"</FONT></FONT><FONT face=Arial color=#000080 size=2>;); <BR>rw.AddResource("Icon", ic); <BR>rw.Close(); <BR></FONT></P> <P><FONT face=Arial color=#000000 size=2>oppure basta scaricarsi il programmino Resources a questo link:<BR></FONT><FONT face=Arial color=#000000 size=2>http://www.aisto.com/roeder/dotnet/</FONT><FONT face=Arial color=#000000 size=2> </FONT></P> <P><FONT face=Arial color=#000000 size=2>A questo basta aggiungere il file resources al&nbsp;vostro progetto. (Menu File-&gt;Aggiungi Elemento Esistente).</FONT></P> <P><FONT face=Arial color=#000000 size=2>Per poter invece caricare un'immagine da file resources:</FONT></P> <P><FONT size=2><FONT color=#000080><FONT face=Arial>ResourceManager rm = </FONT><FONT face=Arial>new ResourceManager("nomeNamespace.NomeFileResources", this</FONT></FONT><FONT face=Arial color=#000080>.GetType().Assembly);<BR></FONT></FONT><FONT face=Arial color=#000080 size=2>Icon iconaForm = (Icon)rm.GetObject("Icon");</FONT></P> <P><FONT face=Arial color=#000000 size=2>La stessa cosa pu&#242; essere fatta per gli oggetti Image.</FONT></P><img src="http://blogs.ugidotnet.org/ProgettoDiInstallazione/aggbug/10414.aspx" width="1" height="1" /> Sara Inzaghi http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/02/07/10414.aspx Mon, 07 Feb 2005 16:19:00 GMT http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/02/07/10414.aspx#feedback 839 http://blogs.ugidotnet.org/ProgettoDiInstallazione/comments/commentRss/10414.aspx http://blogs.ugidotnet.org/ProgettoDiInstallazione/services/trackbacks/10414.aspx Progetto di Installazione http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/02/03/10281.aspx <P><FONT face=Arial size=2>Il seguente articolo chiarisce 4 punti:<BR>1- come creare un progetto di distribuzione (o installazione);<BR>2- come inserire nuove finestre di dialogo al progetto di distribuzione;<BR>3- come estendere la classe Installer (creazione dll) ed associarla al progetto di distribuzione;<BR>4- come poter effettuare il passaggio dei parametri tra il progetto di distribuzione<BR>e la dll che estende la classe Installer.<BR>(Codice C#)</FONT></P> <P><FONT face=Arial size=2>Partiamo dal primo punto, come creare un progetto di distribuzione:<BR>per prima cosa vi conviene creare per esempio una semplice applicazione Windows con C# che chiameremo Test.<BR>Una volta creata, andare nel menu File, cliccare su Aggiungi Progetto -> Nuovo progetto.<BR>Scegliere la voce "Progetto di Installazione e Distribuzione", assegnare un nome al Progetto e dare Ok.<BR>A questo punto si dovrebbe aprire in automatico il File System del progetto di Installazione, se non fosse cos&#236; selezionare il progetto di distribuzione in Esplora Soluzioni e dal menu Visualizza, scegliere la voce Editor->File System.<BR>Sar&#224; possibile vedere 3 cartelle: Cartella Applicazione, Desktop utente e Manu Programmi Utente.<BR>Cliccare su cartella applicazione con il tasto sx del mouse e cliccare su Aggiungi->Output Progetto.</FONT></P> <P><FONT face=Arial size=2>In automatico verr&#224; scelto il progetto da associare, ovvero Test.<BR>Selezionare la voce "Output primario" e dare ok.</FONT></P> <P><FONT face=Arial size=2>A questo punto aggiungiamo una finestra di dialogo al progetto.<BR>Dal menu Visualizza, scegliere la voce Editor-> Interfaccia Utente, cos&#236; facendo possiamo vedere l'elenco delle finestre di dialogo che verranno visualizzate durante l'installazione del programma Test.<BR>Per esempio inseriamo una finestra di dialogo con 2 CheckBox. </FONT></P> <P><FONT face=Arial size=2>Per fare questo bisogna cliccare con il tasto sinistro del mouse sulla voce "Avvio" e cliccare su Agguingi <BR>finestra di dialogo.A questo punto viene visualizzato un elenco di finestre di dialogo che si possono aggiungere, alcune contengono radio button, altre semplici label, ecc...<BR>Nel nostro caso aggiungiamo una Casella di Controllo e la inseriamo tra "Introduzione" e "Cartella di Installazione". <BR>N.B. Per spostare le finestre di dialogo basta trascinarle con il mouse.<BR>Se guardiamo le propriet&#224; della finestra di dialogo possiamo notare che ci sono 4 CheckBox ognuna delle quali ha:<BR>- una Label o etichetta della casella di controllo;<BR>- una Property ovvero il nome della casella di controllo. Questo valore &#232; molto importante perch&#232; servir&#224; quando dovremmo passare dei valori alla classe Installer.<BR>- un valore che indica se la check Box risult&#224; gi&#224; spuntata;<BR>- un valore che indica se la check Box &#232; visibile.<BR>Per es. sulla label della prima check Box scriviamo "Crea File" e sulla label della seconda check box scriviamo "Elimina File" mentre le altre due check box le rendiamo "non visibili".</FONT></P> <P><FONT face=Arial size=2>Passiamo al p.to 3 ovvero alla creazione della dll.<BR>E' necessario avviare un'altro progetto come libreria di classi.<BR>A questo punto la classe la definiamo cos&#236;:</FONT></P> <P><FONT face=Arial color=#000080 size=2>using System;<BR>using System.ComponentModel;<BR>using System.Collections;<BR>using System.Configuration.Install;</FONT></P> <P><FONT face=Arial color=#000080 size=2>namespace XXInstaller<BR>{<BR> [RunInstaller(true)]<BR> public class MyInstaller: Installer<BR> {<BR>  public MyInstaller()<BR>  {<BR>  }<BR>  <BR>  public override void Install(IDictionary savedState)<BR>  {<BR>   base.Install(savedState);<BR>  }</FONT></P> <P><FONT face=Arial color=#000080 size=2>  public override void Commit(IDictionary savedState)<BR>  {<BR>   base.Commit(savedState);<BR>  }</FONT></P> <P><FONT face=Arial color=#000080 size=2>  public override void Rollback(IDictionary savedState)<BR>  {<BR>   base.Rollback(savedState);<BR>  }</FONT></P> <P><FONT face=Arial color=#000080 size=2>  public override void Uninstall(IDictionary savedState)<BR>  {<BR>   base.Uninstall(savedState);<BR>  }<BR> }<BR>}</FONT></P> <P><FONT face=Arial size=2>In questo modo ridefiniamo le funzioni Install, Commit, Rollback e Unistall.<BR>Compiliamo il progetto e generiamo la dll.<BR>La nostra dll per ora non fa niente di particolare..richiama solamente le funzioni della classe Installer.<BR>Attenzione che MSDN dice che &#232; necessario, senza darne un motivo ben preciso, che la chiamata alla classe base avvenga prima del codice "custom"!!!</FONT></P> <P><FONT face=Arial size=2>A questo associamo la dll al nostro progetto di distribuzione.<BR>Visualizzare l'editor del file system e con il tasto destro del mouse cliccare su "Cartella Applicazione" e aggiungere la dll appena creata al progetto di installazione. <BR>A questo punto visualizzare l'editor delle azioni personalizzate, cliccare sempre con il tasto destro del mouse su "Azione personalizzata" e sceglire la voce "Aggiungi azione personalizzata". Viene aperta una finestra dalla quale effettuare la scelta, quindi cliccare su "Cartella Applicazione" e scegliere la dll che &#232; stata aggiunta<BR>precedentemente al progetto di installazione.</FONT></P> <P><FONT face=Arial size=2>4. Ora passiamo ad analizzare come effettuare il passaggio dei parametri dal progetto di installazione<BR>alla dll che estende la classe Installer.<BR>Per prima cosa nel progetto di installazione dobbiamo:<BR>1. visualizzare l'editor delle azioni personalizzate dove precedentemente avevamo associato la nostra dll ai metodi Install, Commit, Rollback e Unistall.<BR>2.Visualizzaziamo le propriet&#224; della dll relativa al metodo Install.<BR>Possiamo notare la propriet&#224; CustomActionData che serve proprio per passare i parametri al metodo Install relativo.<BR>Allora su CustomActionData scriveremo:<BR>/nome1=[CHECKBOXA1] /nome2=[CHECKBOXA2]<BR>dove CHECKBOXA1 e CHECKBOXA2 sono i nomi delle caselle di controllo.<BR>Mentre nome1 e nome2 siamo noi ad assegnarli, sono praticamente i nomi delle variabili che passaremo alla dll.<BR>La propriet&#224; CustomActionData assume il formato /nome=valore. <BR>Pi&#249; valori devono essere separati da uno spazio singolo: /nome1=valore1 /nome2=valore2. <BR>Se un valore ha uno spazio al suo interno, deve essere racchiuso fra virgolette: <BR>/nome="un valore". <BR>Le propriet&#224; di Windows Installer possono essere passate utilizzando le parentesi quadre: /nome=[NOMEPROPRIET&#192;]. </FONT></P> <P><FONT face=Arial size=2>A questo punto passiamo ad aggiungere un po' di codice sul progetto di estensione della classe Installer. In questo caso inserisco solamente il codice delle modifiche al metodo Install:<BR></FONT><FONT face=Arial color=#000080 size=2>public override void Install(IDictionary savedState)<BR>  {<BR>   base.Install(savedState);</FONT></P> <P><FONT face=Arial color=#000080 size=2>   //System.Diagnostics.Debugger.Break();</FONT></P> <P><FONT face=Arial color=#000080 size=2>   string nome1 = this.Context.Parameters["nome1"];<BR>   string nome2 = this.Context.Parameters["nome2"];</FONT></P> <P><FONT face=Arial color=#000080 size=2>   /*<BR>   Le variabili nome1 e nome2 possono assumere valore 1 se sono<BR>   state spuntate, altrimenti possono assumere valore 0 oppure<BR>   valore nullo. Per non dover controllare sempre se nome1 e nome2<BR>   sono uguali a 0 o a null...eseguo l'assegnazione seguente.<BR>   */<BR>   if (nome1 != "1")<BR>    nome1 = "0";</FONT></P> <P><FONT face=Arial color=#000080 size=2>   if (nome2 != "1")<BR>    nome2 = "0";</FONT></P> <P><FONT face=Arial color=#000080 size=2>   /*<BR>    * Se per caso non &#232; spuntata n&#232; nome1 n&#232; nome2 allora blocco l'installazione<BR>    **/</FONT></P> <P><FONT face=Arial color=#000080 size=2>   if (nome1 == "0" && nome2 == "0")<BR>   {<BR>    throw new Exception("Deve essere spuntata almeno una delle due opzioni!");<BR>   }</FONT></P> <P><FONT face=Arial color=#000080 size=2>   if (nome1 == "1")<BR>   {<BR>    //Creazione di un file<BR>    try<BR>    {<BR>     FileInfo fi = new FileInfo(@"C:\testInstaller.txt");<BR>     if (fi.Exists == false)<BR>      fi.Create();<BR>    }<BR>    catch(Exception){}<BR>   }</FONT></P> <P><FONT face=Arial><FONT color=#000080 size=2>   if (nome2=="1")<BR>   {<BR>    //Eliminazione del file<BR>    try<BR>    {<BR>     FileInfo fi = new FileInfo(@"C:\testInstaller.txt");<BR>     if (fi.Exists == true)<BR>      fi.Delete();<BR>    }<BR>    catch(Exception){}<BR>   }<BR>  }<BR>  </FONT></FONT></P> <P><FONT face=Arial size=2>A questo punto ricreare la dll. <BR>Creare il progetto di installazione, fare attenzione a prendere la dll con l'estensione della classe Installer appena creata!<BR>  <BR>A questo punto il nostro progetto di installazione &#232; completato!</FONT></P><img src="http://blogs.ugidotnet.org/ProgettoDiInstallazione/aggbug/10281.aspx" width="1" height="1" /> Sara Inzaghi http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/02/03/10281.aspx Thu, 03 Feb 2005 13:49:00 GMT http://blogs.ugidotnet.org/ProgettoDiInstallazione/archive/2005/02/03/10281.aspx#feedback 252 http://blogs.ugidotnet.org/ProgettoDiInstallazione/comments/commentRss/10281.aspx http://blogs.ugidotnet.org/ProgettoDiInstallazione/services/trackbacks/10281.aspx