RE: Problem in converting to C#



Here's what we get via our Instant C# VB to C# converter (you may have to
make one or two adjustments):

using System.Runtime.InteropServices;
public delegate void delegate1(object s, System.EventArgs e);

public class Form1 : System.Windows.Forms.Form
{
#region Windows Form Designer generated code

public Form1() : base()
{

//This call is required by the Windows Form Designer.
InitializeComponent();

//Add any initialization after the InitializeComponent() call

}

//Form overrides dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}


//Required by the Windows Form Designer
private System.ComponentModel.IContainer components;


//NOTE: The following procedure is required by the Windows Form Designer
//It can be modified using the Windows Form Designer.
//Do not modify it using the code editor.
internal AxSHDocVw.AxWebBrowser Browser;
internal System.Windows.Forms.Splitter Splitter1;
internal System.Windows.Forms.Button Button1;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.TextBox TextBox1;
internal System.Windows.Forms.StatusBar StatusBar1;
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));
this.Browser = new AxSHDocVw.AxWebBrowser();
this.Splitter1 = new System.Windows.Forms.Splitter();
this.Button1 = new System.Windows.Forms.Button();
this.Label1 = new System.Windows.Forms.Label();
this.TextBox1 = new System.Windows.Forms.TextBox();
this.StatusBar1 = new System.Windows.Forms.StatusBar();
((System.ComponentModel.ISupportInitialize)this.Browser).BeginInit();
this.SuspendLayout();
//
//Browser
//
this.Browser.Anchor =
(System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom) |
System.Windows.Forms.AnchorStyles.Left) |
System.Windows.Forms.AnchorStyles.Right));
this.Browser.Enabled = true;
this.Browser.Location = new System.Drawing.Point(8, 32);
this.Browser.OcxState =
(System.Windows.Forms.AxHost.State)(resources.GetObject("Browser.OcxState"));
this.Browser.Size = new System.Drawing.Size(776, 528);
this.Browser.TabIndex = 0;
//
//Splitter1
//
this.Splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.Splitter1.Location = new System.Drawing.Point(0, 0);
this.Splitter1.Name = "Splitter1";
this.Splitter1.Size = new System.Drawing.Size(792, 24);
this.Splitter1.TabIndex = 1;
this.Splitter1.TabStop = false;
//
//Button1
//
this.Button1.Anchor =
(System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Right));
this.Button1.Location = new System.Drawing.Point(752, 0);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(40, 23);
this.Button1.TabIndex = 2;
this.Button1.Text = "Go";
//
//Label1
//
this.Label1.Location = new System.Drawing.Point(0, 3);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(64, 16);
this.Label1.TabIndex = 3;
this.Label1.Text = "Entrer URL";
//
//TextBox1
//
this.TextBox1.Anchor =
(System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom) |
System.Windows.Forms.AnchorStyles.Left) |
System.Windows.Forms.AnchorStyles.Right));
this.TextBox1.Location = new System.Drawing.Point(64, 0);
this.TextBox1.Name = "TextBox1";
this.TextBox1.Size = new System.Drawing.Size(680, 20);
this.TextBox1.TabIndex = 4;
this.TextBox1.Text = "http://my.mindvision.com/~ben/tools/";;
//
//StatusBar1
//
this.StatusBar1.Location = new System.Drawing.Point(0, 544);
this.StatusBar1.Name = "StatusBar1";
this.StatusBar1.Size = new System.Drawing.Size(792, 22);
this.StatusBar1.TabIndex = 5;
//
//Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(792, 566);
this.Controls.Add(this.StatusBar1);
this.Controls.Add(this.TextBox1);
this.Controls.Add(this.Label1);
this.Controls.Add(this.Button1);
this.Controls.Add(this.Splitter1);
this.Controls.Add(this.Browser);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)this.Browser).EndInit();
this.ResumeLayout(false);


//INSTANT C# NOTE: Converted event handlers:
base.Load += new System.EventHandler(Form1_Load);
Button1.Click += new System.EventHandler(Button1_Click);
Browser.BeforeNavigate2 += new System.EventHandler(Browser_BeforeNavigate2);
Browser.NavigateComplete2 += new
System.EventHandler(Browser_NavigateComplete2);
Browser.DownloadBegin += new System.EventHandler(Browser_DownloadBegin);
Browser.DownloadComplete += new
System.EventHandler(Browser_DownloadComplete);

}

#endregion

public event delegate1 e;
public HttpWebRequest myReq;
private void Form1_Load(object sender, System.EventArgs e)
{
}
private System.Int32 m_DownloadManagerCookie;
private void SetDownloadManager()
{
COMVB.IProfferService ips = null;
object objBrowser = null;
COMVB.UCOMIServiceProvider ispBrowser = null;
IntPtr ip = null;
try
{
// Get a handle to the browser ocx
objBrowser = Browser.GetOcx();
ispBrowser = (COMVB.UCOMIServiceProvider)objBrowser;


// Query the browser for the IProfferService interface
ip = ispBrowser.QueryService(ref COMVB.SID_SProfferService, ref
COMVB.IID_IProfferService);
ips = (COMVB.IProfferService)(Marshal.GetObjectForIUnknown(ip));


// Proffer the new object as the provider of the IHTMLEditHostinterface
handler
ips.ProfferService(ref COMVB.IID_IDownloadManager, new ServiceProvider(),
ref m_DownloadManagerCookie);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
object url = TextBox1.Text;
object o = null;
HttpWebRequest myReq = WebRequest.Create(url);
Browser.Navigate2(url, o, o, o, o);
SetDownloadManager();
}


private void Browser_BeforeNavigate2(object sender,
AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e)
{
//MsgBox(e.uRL & " ")
RemoteUrl = e.uRL;
//Dim x As New AxSHDocVw.AxWebBrowser

}

private void Browser_NavigateComplete2(object sender,
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{

}

public void Browser_DownloadBegin(object sender, System.EventArgs e)
{
//MsgBox(RemoteUrl.ToString())
}

private void Browser_DownloadComplete(object sender, System.EventArgs e)
{

}
}


internal class ServiceProvider : COMVB.UCOMIServiceProvider
{

public IntPtr QueryService(ref System.Guid guidService, ref System.Guid riid)
{

DownloadTestHarnessForm f = new DownloadTestHarnessForm();
f.Show();


//Dim ()arr As String = riid.GetType().GetFields


//MsgBox(riid)


//If guidService.Equals(COMVB.IID_IDownloadManager) Then
// 'MessageBox.Show("Now only the IDownloadManager Called")
// If riid.Equals(COMVB.IID_IDownloadManager) Then
// Dim idm As COM.Interfaces.IDownloadManager


// idm = New DownloadManager


// Return GetInterface(idm, COMVB.IID_IDownloadManager)
// Else
// Return Nothing
// End If
//Else
// Return Nothing
//End If

//INSTANT C# NOTE: Inserted the following 'return' since all code paths
must return a value in C#:
return null;
}

public IntPtr GetInterface(object obj, ref Guid iid)
{

IntPtr pUnk = null;
IntPtr pInterface = null;
int hr = 0;

pUnk = Marshal.GetIUnknownForObject(obj);
hr = Marshal.QueryInterface(pUnk, ref iid, out pInterface);

if (hr < 0)
Marshal.ThrowExceptionForHR(hr);

Marshal.Release(pUnk);

return pInterface;

}

}


internal class DownloadManager : COM.Interfaces.IDownloadManager
{

public int Download(System.Runtime.InteropServices.UCOMIMoniker pmk,
System.Runtime.InteropServices.UCOMIBindCtx pbc, System.UInt32 dwBindVerb,
int grfBINDF, System.IntPtr pBindInfo, string pszHeaders, string pszRedir,
System.UInt32 uiCP)
{
return new System.Int32();
}

}


internal class COMVB
{
// IServiceProvider interface
[ComVisible(true), ComImport(),
Guid("6d5140c1-7436-11ce-8034-00aa006009fa"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface UCOMIServiceProvider
{
IntPtr QueryService(ref Guid guidService, ref Guid riid);
}

// IProfferService interface
[ComVisible(true), ComImport(),
Guid("cb728b20-f786-11ce-92ad-00aa00a74cd0"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IProfferService
{
void ProfferService(ref Guid guidService, UCOMIServiceProvider psp, ref
int cookie);
void RevokeService(int cookie);
}
public static Guid SID_SProfferService = new
Guid("cb728b20-f786-11ce-92ad-00aa00a74cd0");
public static Guid IID_IProfferService = new
Guid("cb728b20-f786-11ce-92ad-00aa00a74cd0");
public static Guid SID_SHTMLEditHost = new
Guid("3050f6a0-98b5-11cf-bb82-00aa00bdce0b");
public static Guid IID_IDownloadManager = new
Guid("988934A4-064B-11D3-BB80-00104B35E7F9");
}

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#


"kamal" wrote:

This is the code i have. By usin this code we can overload the
DownloadManager of the Internet explorer. Te class ServiceProvider have the
function query service is returning some pointer in vb.net. We have
commented that total code and there we displayed our own form to download
the file.

But when it is converted into C# the function QueryService is not returning
a pointer if it returns the pointer the default IE download Manager Dialog
Box is appearing. How can i solve this problem. It has to work same like in
vb.net after converting it into C#. Any solutions. To run this vb
application have to import the COM.dll i am also attatching the dll file. to
run this vb code have to add web browser and rename that web browser name as
Browser.



Thanks in advance.



Imports System.Runtime.InteropServices
Public Delegate Sub delegate1(ByVal s As System.Object, ByVal e As
System.EventArgs)



Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "



Public Sub New()
MyBase.New()



'This call is required by the Windows Form Designer.
InitializeComponent()



'Add any initialization after the InitializeComponent() call



End Sub



'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub



'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer



'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Browser As AxSHDocVw.AxWebBrowser
Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.Browser = New AxSHDocVw.AxWebBrowser
Me.Splitter1 = New System.Windows.Forms.Splitter
Me.Button1 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.StatusBar1 = New System.Windows.Forms.StatusBar
CType(Me.Browser,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Browser
'
Me.Browser.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top
Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.Browser.Enabled = True
Me.Browser.Location = New System.Drawing.Point(8, 32)
Me.Browser.OcxState = CType(resources.GetObject("Browser.OcxState"),
System.Windows.Forms.AxHost.State)
Me.Browser.Size = New System.Drawing.Size(776, 528)
Me.Browser.TabIndex = 0
'
'Splitter1
'
Me.Splitter1.Dock = System.Windows.Forms.DockStyle.Top
Me.Splitter1.Location = New System.Drawing.Point(0, 0)
Me.Splitter1.Name = "Splitter1"
Me.Splitter1.Size = New System.Drawing.Size(792, 24)
Me.Splitter1.TabIndex = 1
Me.Splitter1.TabStop = False
'
'Button1
'
Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Button1.Location = New System.Drawing.Point(752, 0)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(40, 23)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Go"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(0, 3)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(64, 16)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Entrer URL"
'
'TextBox1
'
Me.TextBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top
Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.TextBox1.Location = New System.Drawing.Point(64, 0)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(680, 20)
Me.TextBox1.TabIndex = 4
Me.TextBox1.Text = "http://my.mindvision.com/~ben/tools/";
'
'StatusBar1
'
Me.StatusBar1.Location = New System.Drawing.Point(0, 544)
Me.StatusBar1.Name = "StatusBar1"
Me.StatusBar1.Size = New System.Drawing.Size(792, 22)
Me.StatusBar1.TabIndex = 5
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(792, 566)
Me.Controls.Add(Me.StatusBar1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Splitter1)
Me.Controls.Add(Me.Browser)
Me.Name = "Form1"
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
CType(Me.Browser,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)



End Sub



#End Region



Public Event e As delegate1
Public myReq As HttpWebRequest
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
Private m_DownloadManagerCookie As System.Int32
Private Sub SetDownloadManager()
Dim ips As COMVB.IProfferService
Dim objBrowser As Object
Dim ispBrowser As COMVB.UCOMIServiceProvider
Dim ip As IntPtr
Try
' Get a handle to the browser ocx
objBrowser = Browser.GetOcx()
ispBrowser = DirectCast(objBrowser, COMVB.UCOMIServiceProvider)



' Query the browser for the IProfferService interface
ip = ispBrowser.QueryService(COMVB.SID_SProfferService,
COMVB.IID_IProfferService)
ips = DirectCast(Marshal.GetObjectForIUnknown(ip),
COMVB.IProfferService)



' Proffer the new object as the provider of the
IHTMLEditHostinterface handler
ips.ProfferService(COMVB.IID_IDownloadManager, New
ServiceProvider, m_DownloadManagerCookie)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim url As System.Object = TextBox1.Text
Dim o As System.Object = Nothing
Dim myReq As HttpWebRequest = WebRequest.Create(url)
Browser.Navigate2(url, o, o, o, o)
SetDownloadManager()
End Sub



Private Sub Browser_BeforeNavigate2(ByVal sender As Object, ByVal e As
AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event) Handles
Browser.BeforeNavigate2
'MsgBox(e.uRL & " ")
RemoteUrl = e.uRL
'Dim x As New AxSHDocVw.AxWebBrowser



End Sub



Private Sub Browser_NavigateComplete2(ByVal sender As Object, ByVal e As
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles
Browser.NavigateComplete2



End Sub



Public Sub Browser_DownloadBegin(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Browser.DownloadBegin
'MsgBox(RemoteUrl.ToString())
End Sub



Private Sub Browser_DownloadComplete(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Browser.DownloadComplete



End Sub
End Class



Class ServiceProvider
Implements COMVB.UCOMIServiceProvider




Public Function QueryService(ByRef guidService As System.Guid, ByRef
riid As System.Guid) As IntPtr Implements
COMVB.UCOMIServiceProvider.QueryService



Dim f As New DownloadTestHarnessForm
f.Show()



'Dim ()arr As String = riid.GetType().GetFields



'MsgBox(riid)



'If guidService.Equals(COMVB.IID_IDownloadManager) Then
' 'MessageBox.Show("Now only the IDownloadManager Called")
' If riid.Equals(COMVB.IID_IDownloadManager) Then
' Dim idm As COM.Interfaces.IDownloadManager



' idm = New DownloadManager



.


Loading