By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

Calling a C# DLL from VB6

P: n/a
I apologize for using this newsgroup for what seems like a VB6 question, but
I did not see a newsgroup for VB6. I also think I may not have the C# code
setup correctly for calling from VB6. If anyone can please help, I would
greatly appreciate it.

I have a C# DLL, code below that I am trying to call from a VB6 program. I
read an article that said to run RegAsm.exe from the .Net SDK and this would
enable you to reference the C# DLL from within VB6. But when I try it, I get
the following error.

File or assembly name WCSPrintServer, or one of its dependencies, was not
found.

The VB6 code is a simple form with a command button and the following code.

Dim oPrintServer As New WCSPrintServer.PrintServer

oPrintServer.PrintReport "WCSReport", "\\webproxy\HP LaserJet 4100", "25",
"Durand", "Aurora", "Street", "3", "123456789"

Can anyone please tell me what I am doing wrong?
----------------------WCSPrintServer.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Web.Services.Protocols;
using System.Runtime.InteropServices; // For Marshal.Copy
using System.Xml;

namespace WCSComponents
{
public class PrintServer
{
ReportingService rs;
private byte[][] m_renderedReport;
private Graphics.EnumerateMetafileProc m_delegate = null;
private MemoryStream m_currentPageStream;
private Metafile m_metafile = null;
int m_numberOfPages;
private int m_currentPrintingPage;
private int m_lastPrintingPage;
private ParameterValue[] reportParameters;
private string m_ErrorDescription;

public PrintServer()
{
//Public constructor needed for the .Net application to be called from a
COM component.
}
public string ErrorMessage
//This allows the calling program to get information about any errors
//that may have happened.
{
get
{
return m_ErrorDescription;
}
set
{
m_ErrorDescription = value;
}
}
public bool PrintReport(string reportPath, string printer, string
transactionID, string jurorName,
string address1, string address2, string daysServed, string
participantNumber)
{
int iArrayCount = 6;

try
{
reportParameters = new ParameterValue[6];

for (int iCount = 0; iCount < iArrayCount ; iCount++)
{
reportParameters[iCount] = new ParameterValue();

switch (iCount)
{
case 0:
reportParameters[iCount].Name = "TransactionID";
reportParameters[iCount].Value = transactionID;
break;

case 1:
reportParameters[iCount].Name = "JurorName";
reportParameters[iCount].Value = jurorName;
break;

case 2:
reportParameters[iCount].Name = "Address1";
reportParameters[iCount].Value = address1;
break;

case 3:
reportParameters[iCount].Name = "Address2";
reportParameters[iCount].Value = address2;
break;

case 4:
reportParameters[iCount].Name = "DaysServed";
reportParameters[iCount].Value = daysServed;
break;

case 5:
reportParameters[iCount].Name = "ParticipantNumber";
reportParameters[iCount].Value = participantNumber;
break;

default:
//Nothing
break;
}
}

//Create proxy object and authenticate.
rs = new ReportingService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
this.RenderedReport = this.RenderReport(reportPath, reportParameters);

try
{
bool printLandscape = false;
printLandscape = IsLandscape(reportPath);

byte[] reportDefinition = null;
reportDefinition = rs.GetReportDefinition(reportPath);

//Wait for the report to completely render.
if(m_numberOfPages < 1)
return false;
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.MaximumPage = m_numberOfPages;
printerSettings.MinimumPage = 1;
printerSettings.PrintRange = PrintRange.SomePages;
printerSettings.FromPage = 1;
printerSettings.ToPage = m_numberOfPages;
printerSettings.PrinterName = printer;
PrintDocument pd = new PrintDocument();
m_currentPrintingPage = 1;
m_lastPrintingPage = m_numberOfPages;
pd.PrinterSettings = printerSettings;
pd.DefaultPageSettings.Landscape = printLandscape;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
return true;
}

catch(Exception ex)
{
m_ErrorDescription = ex.Message;
Console.WriteLine(ex.Message);
return false;
}

finally
{

}
}

catch(System.Exception e)
{
string exceptionText;
exceptionText = e.ToString();
m_ErrorDescription = e.Message;
return false;
}

finally
{
//Clean up goes here
}
}
private byte[][] RenderReport(string reportPath, ParameterValue[]
reportParameters)
{
string deviceInfo = null;
string format = "IMAGE";
Byte[] firstPage = null;
string encoding;
string mimeType;
Warning[] warnings = null;
ParameterValue[] reportHistoryParameters = null;
string[] streamIDs = null;
Byte[][] pages = null;

// Build device info based on the start page
deviceInfo =
String.Format(@"<DeviceInfo><OutputFormat>{0}</OutputFormat></DeviceInfo>",
"emf");

//Exectute the report and get page count.
try
{
// Renders the first page of the report and returns streamIDs for
// subsequent pages
firstPage = rs.Render(
reportPath,
format,
null,
deviceInfo,
reportParameters,
null,
null,
out encoding,
out mimeType,
out reportHistoryParameters,
out warnings,
out streamIDs);
// The total number of pages of the report is 1 + the streamIDs
m_numberOfPages = streamIDs.Length + 1;
pages = new Byte[m_numberOfPages][];

// The first page was already rendered
pages[0] = firstPage;

for (int pageIndex = 1; pageIndex < m_numberOfPages; pageIndex++)
{
// Build device info based on start page
deviceInfo =
String.Format(@"<DeviceInfo><OutputFormat>{0}</OutputFormat><StartPage>{1}</StartPage></DeviceInfo>", "emf", pageIndex+1);
pages[pageIndex] = rs.Render(reportPath, format, null, deviceInfo,
reportParameters, null, null, out encoding, out mimeType,
out reportHistoryParameters, out warnings, out streamIDs);
}
}
catch (SoapException ex)
{
m_ErrorDescription = ex.Detail.InnerXml;
Console.WriteLine(ex.Detail.InnerXml);
}
catch (Exception ex)
{
m_ErrorDescription = ex.Message;
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Number of pages: {0}", pages.Length);
}
return pages;
}
private bool IsLandscape(string psReportpath)
{
string rdl;
string sheight = "", swidth = "";
byte[] reportDefinition = null;
double Height;
double Width;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

try
{

reportDefinition = rs.GetReportDefinition(psReportpath);
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
rdl = enc.GetString(reportDefinition);
doc.LoadXml(rdl);
XmlNodeList elemList = doc.GetElementsByTagName ("PageHeight");

int i;
for (i = 0 ; i<elemList[0].InnerXml.Length; i++)
{
if ((elemList[0].InnerXml[i] >= '0' && elemList[0].InnerXml[i] <=
'9')|| elemList[0].InnerXml[i] <= '.' )
{
//= elemList[0].InnerXml[i];
}
else { break; }
}
sheight= elemList[0].InnerXml.Substring(0,i);
Height = Convert.ToDouble (sheight);

elemList = doc.GetElementsByTagName ("PageWidth");
for (i = 0 ; i<elemList[0].InnerXml.Length;i++)
{
if ((elemList[0].InnerXml[i] >= '0' && elemList[0].InnerXml[i] <=
'9')|| elemList[0].InnerXml[i] <= '.' )
{
}
else { break; }
}
swidth= elemList[0].InnerXml.Substring(0,i);
swidth= swidth.Replace (".",",");
Width = Convert.ToDouble (swidth);

if (Height < Width)
{
return true;
}
else
{
return false;
}
}

catch(Exception ex)
{
string exception;
exception = ex.Message;
m_ErrorDescription = ex.Message;
return false;
}
finally
{
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.HasMorePages = false;
if (m_currentPrintingPage <= m_lastPrintingPage &&
MoveToPage(m_currentPrintingPage))
{
// Draw the page
ReportDrawPage(ev.Graphics);
// If the next page is less than or equal to the last page,
// print another page.
if (++m_currentPrintingPage <= m_lastPrintingPage)
ev.HasMorePages = true;
}
}
private void ReportDrawPage(Graphics g)
{
// Method to draw the current emf memory stream
if(null == m_currentPageStream || 0 == m_currentPageStream.Length || null
==m_metafile)
return;
lock(this)
{
// Set the metafile delegate.
int width = m_metafile.Width;
int height= m_metafile.Height;
m_delegate = new Graphics.EnumerateMetafileProc(MetafileCallback);
// Draw in the rectangle
Point[] points = new Point[3];
Point destPoint = new Point(0, 0);
Point destPoint1 = new Point(width, 0);
Point destPoint2 = new Point(0, height);

points[0]=destPoint;
points[1]=destPoint1;
points[2]=destPoint2;

g.EnumerateMetafile(m_metafile, points , m_delegate);
// Clean up
m_delegate = null;
}
}
private bool MoveToPage(Int32 page)
{
// Check to make sure that the current page exists in
// the array list
if(null == this.RenderedReport[m_currentPrintingPage-1])
return false;
// Set current page stream equal to the rendered page
m_currentPageStream = new
MemoryStream(this.RenderedReport[m_currentPrintingPage-1]);
// Set its postion to start.
m_currentPageStream.Position = 0;
// Initialize the metafile
if(null != m_metafile)
{
m_metafile.Dispose();
m_metafile = null;
}
// Load the metafile image for this page
m_metafile = new Metafile((Stream)m_currentPageStream);
return true;
}
private bool MetafileCallback(EmfPlusRecordType recordType, int flags, int
dataSize, IntPtr data, PlayRecordCallback callbackData)
{
byte[] dataArray = null;
// Dance around unmanaged code.
if (data != IntPtr.Zero)
{
// Copy the unmanaged record to a managed byte buffer
// that can be used by PlayRecord.
dataArray = new byte[dataSize];
Marshal.Copy(data, dataArray, 0, dataSize);
}
// play the record.
m_metafile.PlayRecord(recordType, flags, dataSize, dataArray);

return true;
}
private byte[][] RenderedReport
{
get
{
return m_renderedReport;
}
set
{
m_renderedReport = value;
}
}

}
}

Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.