473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Calling a C# DLL from VB6

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
0 2541

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. ...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.