1. Tracy Lamb
  2. PowerBuilder
  3. Saturday, 25 June 2022 19:06 PM UTC

Hi all,

With all the problems I was having with the Brother printer COM server, I thought I'd try writing my own in C# (VS2015). Mine is very simple right now. It just prints a sample label.  I wrote a second C# console program to test... works fine. Demonstrating to myself that the Brother COM object works as advertised, just not in PB.  The label prints as expected.  (Sorry, I forget the reason why I had to go from a simple dll to a com object.)

I found a similar question in Q&A from about 4 years ago.  Rodolfo Reyes provided a great link that shows how to make and install a COM object from VS. 

 http://blogs.artinsoft.net/Mrojas/archive/2009/03/03/Calling-NET-from-PowerBuilder.aspx

I followed all of the directions... but in my PB app I keep getting error code -2: Class name not found when I'm trying to connect to the object.

OLEObject label
int li_rc
boolean lb_rc

label = CREATE OLEObject
li_rc = label.ConnectToNewObject("PTouch.PTouch")

if li_rc<> 0 then
   MessageBox("Error","Component Installation Error: " + string(li_rc))
else
   lb_rc = label.PrintLabel()
end if

destroy label
return

In VS2015, I've got a Namespace named  "PTouch" and I've got a Class named "Ptouch" and I have a Method named "PrintLabel".

namespace PTouch
{
   [ComVisible(true)]
   [ClassInterface(ClassInterfaceType.AutoDual)]
   [ProgId("PTouch:PTouch")]

   public class PTouch
   {
      private bool lb_rc = false;
      private string strLabel = "";
      public bool PrintLabel() 
      {
          strLabel = "C:\\BenchTop10\\Standard 1in.lbx";
          bpac.Document doc = new Document();

etc...

Here's the PB Code...

OLEObject label
int li_rc
boolean lb_rc

label = CREATE OLEObject
li_rc = label.ConnectToNewObject("PTouch.PTouch")

if li_rc<> 0 then
MessageBox("Error","Component Installation Error: " + string(li_rc))
else
lb_rc = label.PrintLabel()
end if

destroy label
return

 

Any ideas why I'm getting Error -2, Class Name not Found?

~~~Tracy

 

 

 

Accepted Answer
Tracy Lamb Accepted Answer Pending Moderation
  1. Tuesday, 9 August 2022 13:05 PM UTC
  2. PowerBuilder
  3. # Permalink

My solution isn't optimal, but I don't like to leave questions unanswered for too long.  My solution was to just write the program in VS2022/C# and create a separate installer.  I figured out how to send and receive a parameter when running a program.  So within my PB app, I RUN the label program and send in a WorkOrder number.  The label program sees the parameter and retrieves the right information from the database.  The user just selects the right label template and printer, then clicks print.  The C# program saves the template & printer to a user file for next time. If a parameter was sent in from the RUN command, the program automatically closes after printing the label.  This let's my application print a label and close.  It also let's the user run the label program independently from my PB app, which is an added benefit.  

 

~~~Tracy

 

Comment
There are no comments made yet.
Andreas Mykonios Accepted Answer Pending Moderation
  1. Monday, 27 June 2022 06:38 AM UTC
  2. PowerBuilder
  3. # 1

Hi Tracy.

If you are using PB 2019 R2 you should take a look in .NET Dll Importer. You should be able to make your c# class work with it. The advantage is that you don't have to register anything in any pc. All that you need is to have the equivalent .net runtime so that your code can be executed and to delploy your dll with your application in the appropriate place (that part should be done also with your com dll). There are many answered questions on this forum about .NET Dll importer that may help you if you have a problem.

That way you don't need to make your dll com visible. You do work with a wizard that makes most of the work for you. If you want to take a look to that feature then you can see following links in documentation:

Calling .NET Assembly - - What's New (appeon.com)

Here are some advices to work with unsupported features.

Adding an adapter for unsupported features - - Application Techniques (appeon.com)

But all this works with PB 2019 R2+.

Andreas.

Comment
  1. Tracy Lamb
  2. Monday, 27 June 2022 22:28 PM UTC
So the .NET Dll Import utility works great! So easy to use. I had to do it twice. The second time I selected "Encapsulate .NET Assembly" advanced option. When I run the code, the PrintLabel() function returns TRUE (Yeah!)... but nothing prints. Lol! I may have to take another look at my simple VS2015 C# progam. Will follow up when that's done, probably tomorrow... it's Happy Hour here!

~~~Tracy

p.s. here's my PB code:

nvo_ptouch invo_myLabel

boolean lb_rc



invo_myLabel = CREATE nvo_ptouch

lb_rc = invo_myLabel.of_printlabel()

MessageBox("of_printlabel",string(lb_rc))

DESTROY invo_myLabel

  1. Helpful
  1. Andreas Mykonios
  2. Tuesday, 28 June 2022 11:09 AM UTC
I hope you find why it wasn't printing. The whole procedure is really simpler than working with com objects.

Andreas.
  1. Helpful
  1. Tracy Lamb
  2. Tuesday, 28 June 2022 12:53 PM UTC
Not seeing anything wrong with the VS 2015/C# code. I wrote a little console app to test the PTouch.dll... that still works just fine.

using PTouch;

namespace ConsoleApplication1

{

class Program

{

static void Main()

{

bool lb_rc;

PTouch.PTouch label = new PTouch.PTouch();

lb_rc = label.PrintLabel();

Console.WriteLine(lb_rc.ToString());

Console.Read();

}

}

}

  1. Helpful
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Saturday, 25 June 2022 20:52 PM UTC
  2. PowerBuilder
  3. # 2

Hi, Tracy - 

Just a guess... Have you registered the COM server with Windows, via the regsvr32.exe utility?

From a quick look at the ConnectToNewObject and ConnectToObject Help topics, you must include the class name argument unless Windows understands what application is associated with the filetype of the named file.

Hope this helps, John

Comment
  1. Chris Pollach @Appeon
  2. Sunday, 26 June 2022 16:02 PM UTC
Hi Tracy;

It's a lot more complicated than that. Not only the registry entry but you need TLB file & more. Here is an article that might be of more use...

https://anvil-of-time.com/powerbuilder/getting-your-com-component-registered-for-use-with-powerbuilder

HTH

Regards... Chris
  1. Helpful 1
  1. Tracy Lamb
  2. Monday, 27 June 2022 21:58 PM UTC
Thanks Chris,

I saw this article before... When I run the regasm I get an Access Denied error. Not sure if I got this error when I did it last week.

~~~Tracy

  1. Helpful
  1. Tracy Lamb
  2. Monday, 27 June 2022 22:00 PM UTC
Trying to find out what an entry-point is in a VS2015 com object project has been very frustrating. There's no Main() function, just the one PrintLabel() function I created. Couldn't find anywhere that I could set an entry point.

~~~Tracy

  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.