I am tring to write simple console application in C# to test the APIs
functions made available by the dymo sdk. The dymo sdk provides a dll
library that can be used to call functions that will print labels on
the DYMO LabelWriter printer.
Here is my C# code. Please let what am I doing wrong. This code is
successfully compiled, but it does not work when you execute the final
executable program. I have included the c structure and functions
inside /* */ codes and below that you will see the C# codes.
using System;
using System.Runtime.InteropServices;
// csc /target:exe /optimize+ /nologo dymo.cs
// csc /target:library /optimize+ /nologo dymo.cs
/*
typedef struct tagLABELINFO {
char LabelName[64];
char PaperName[64];
POINT PaperSize;
POINT BitmapSize;
int LabelCount;
POINT LabelSize;
} TLABELINFO;
typedef TLABELINFO * PLABELINFO;
*/
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct tagLABELINFO
{
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 ) ]
public String LabelName;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 ) ]
public String PaperName;
public int PaperSize;
public int BitmapSize;
public int LabelCount;
public int LabelSize;
} // tagLABELINFO
/*
typedef int TObjectID;
*/
[ StructLayout( LayoutKind.Sequential ) ]
public struct TObjectID
{
public int oID;
} // TObjectID
/*
typedef struct tagObjectInfo {
TObjectID ObjID;
Int ObjType;
char ObjName[64];
RECT Size;
int Rotate;
} TOBJECTINFO;
typedef TOBJECTINFO * POBJECTINFO;
*/
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct tagObjectInfo
{
public TObjectID ObjID;
public int ObjType;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 ) ]
public String ObjName;
public int Size;
public int Rotate;
} // tagObjectInfo
/*
typedef struct tagTTextBlockAttributes {
char * Text;
char Font1[64];
char Font2[64];
int Justify;
int VertJustify;
BOOL bMirrored;
BOOL bVerticalText;
COLORREF TextColor;
COLORREF BackgroundColor;
BOOL IsRichText;
Int Effects;
} TTextBlockAttributes;
typedef TTextBlockAttributes * PTextBlockAttributes;
*/
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct TTextBlockAttributes
{
[ MarshalAs( UnmanagedType.LPStr ) ]
public String Text;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 ) ]
public String Font1;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 ) ]
public String Font2;
public int Justify;
public int VertJustify;
public bool bMirrored;
public bool bVerticalText;
public int TextColor;
public int BackgroundColor;
public bool IsRichText;
public int Effects;
} // TTextBlockAttributes
/*
typedef struct tagTAddressAttributes {
PTextBlockAttributes TextInfo;
int BarCodePos;
BOOL b9DigitOnly;
BOOL bFixedAddr;
BOOL bFormatted;
} TAddressAttributes;
typedef TAddressAttributes * PAddressAttributes;
*/
[ StructLayout( LayoutKind.Sequential ) ]
public struct TAddressAttributes
{
public IntPtr TextInfo;
public int BarCodePos;
public int b9DigitOnly;
public int bFixedAddr;
public int bFormatted;
} // TAddressAttributes
/*
typedef struct tagTGraphicAttributes {
int GraphicSource;
char FileName[260];
HANDLE Picture;
HPALETTE Palette;
HWND Window;
int Border;
COLORREF BorderColor;
} TGraphicAttributes;
typedef TGraphicAttributes * PGraphicAttributes;
*/
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct TGraphicAttributes
{
public int GraphicSource;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=260 ) ]
public String FileName;
public IntPtr Picture;
public int Palette;
public int Window;
public int Border;
public int BorderColor;
} // TGraphicAttributes
/*
typedef struct tagTLineAttributes {
int Length;
int Orient;
int Thickness;
COLORREF LineColor;
} TLineAttributes;
typedef TLineAttributes * PLineAttributes;
*/
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct TLineAttributes
{
public int Length;
public int Orient;
public int Thickness;
public int LineColor;
} // TLineAttributes
/*
typedef struct tagTCounterAttributes {
PTextBlockAttributes TextInfo;
char PreText[32];
char PostText[32];
int Start;
int Current;
int Width;
int Increment;
BOOL bLeadingZeros;
} TCounterAttributes;
typedef TCounterAttributes * PCounterAttributes;
*/
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct TCounterAttributes
{
public TTextBlockAttributes TextInfo;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 ) ]
public String PreText;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 ) ]
public String PostText;
public int Start;
public int Current;
public int Width;
public int Increment;
bool bLeadingZeros;
} // TCounterAttributes
/*
typedef struct tagTDateTimeAttributes {
PTextBlockAttributes TextInfo;
char PreText[32];
char PostText[32];
int DateFormat;
BOOL bIncludeTime;
BOOL b24Hour;
} TDateTimeAttributes;
typedef TDateTimeAttributes * PDateTimeAttributes;
*/
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct TDateTimeAttributes
{
public TTextBlockAttributes TextInfo;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 ) ]
public String PreText;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 ) ]
public String PostText;
public int DateFormat;
public bool bIncludeTime;
public bool b24Hour;
} // TDateTimeAttributes
/*
typedef struct tagTBarcodeAttributes {
char Text[256];
char Font[64];
int HRTextPos;
int BCType;
int BCRatio;
int Justify;
TObjectID Link;
} TBarcodeAttributes;
typedef TBarcodeAttributes * PBarcodeAttributes;
*/
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct tagTBarcodeAttributes
{
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=256 ) ]
public String Text;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 ) ]
public String Font;
public int HRTextPos;
public int BCType;
public int BCRatio;
public int Justify;
public TObjectID Link;
} // TBarcodeAttributes
public class dymo
{
/*
BOOL WINAPI PrintLabel(LPCSTR DeviceName, LPCSTR Port, int
Quantity, BOOL bShowDialog);
*/
[DllImport("labels.dll")]
static extern bool PrintLabel(String DeviceName, String Port, int
Quantity, bool bShowDialog);
/*
int WINAPI ValidateBarCode(char *Text, int BCType);
*/
[DllImport("labels.dll")]
static extern int ValidateBarCode(String Text, int BCType);
/*
BOOL WINAPI DeleteLabelObject(TObjectID ID);
*/
[DllImport("labels.dll")]
static extern bool DeleteLabelObject(IntPtr ID);
/*
HANDLE WINAPI AddObject(LPCSTR ObjType, LPCSTR ObjName, RECT Size,
int Rotation, void * Attrib);
*/
[DllImport("labels.dll")]
static extern IntPtr AddObject(String ObjType, String ObjName, int
Size, int Rotation, IntPtr Attrib);
/*
void WINAPI NewLabel(LPCSTR Name);
*/
[DllImport("labels.dll")]
static extern void NewLabel(String Name);
/*
BOOL WINAPI SetAttributes(TObjectID ID, void * Attrib);
*/
[DllImport("labels.dll")]
static extern bool SetAttributes(ref TObjectID ID, IntPtr Attrib);
[STAThread]
static void Main(string[] args)
{
// create a new label
NewLabel("sunday");
TTextBlockAttributes txtAttribute = new TTextBlockAttributes();
txtAttribute.Text = "Hello World \r\n";
txtAttribute.Font1 = "Times New Roman, 10, Bold" + '\0';
txtAttribute.Font2 = "Times New Roman, 10, Bold" + '\0';
txtAttribute.Justify = 0;
txtAttribute.VertJustify = 0;
txtAttribute.bMirrored = false;
txtAttribute.bVerticalText = false;
txtAttribute.TextColor = 0;
txtAttribute.BackgroundColor = 255;
txtAttribute.IsRichText = false;
txtAttribute.Effects = 0;
TObjectID to = new TObjectID();
IntPtr mypointer =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TTextBl ockAttributes)));
Marshal.StructureToPtr(txtAttribute, mypointer, true);
// IntPtr item = Marshal.PtrToStructure( txtAttribute,
typeof(IntPtr));
to.oID = (int) AddObject("Text", "sunday", 0, 0, mypointer);
Console.WriteLine("the new object ID is {0}", to.oID);
bool printed = PrintLabel("DYMO LabelWriter 330 Turbo-USB",
null, 1, false);
printed = DeleteLabelObject(to.oID);
Marshal.FreeHGlobal(mypointer);
Console.WriteLine("deleted is {0}", printed);
} // end of Main()
} // end of class