473,320 Members | 1,865 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,320 software developers and data experts.

passing a structure to unmanaged code

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

Nov 17 '05 #1
0 3814

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

Similar topics

2
by: Chris | last post by:
Hi, I seem to be having a problem I can't quite figure out. Currently, I have the following code (below). In the SendMsg function, I create a 'Qtkmsg' which converts the string 'text' into a...
5
by: adrin | last post by:
hi i have the following problem: i use functions from an external dll(normal w32 unmanaged library) which takes a structure address as an argument.. how do i pass it in c#? i tried using array...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
5
by: GeRmIc | last post by:
Hi, I am doing an interop from unmanaged code to C#. How do i pass an ArrayList pointer from an unmanaged code, (structres are easily passed by between C# and C). //This is the C code ...
0
by: nygiantswin2005 | last post by:
I would like to know how do I pass a pointer to a struct from managed code to unmanaged code. For example if I create structure like this in managed code. StructLayout( LayoutKind.Sequential,...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
15
by: John Alway | last post by:
Hello, I'm using a DLL I wrote in C++, and am attempting to call and use it from VB. This works fine for functions where I pass parameters by value, but I can't get pointers to work. I get...
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
5
by: craig1231 | last post by:
I have a problem trying to pass a structure to an unmanaged c++ DLL from C#. When I call PCSBSpecifyPilotLogon from C#, it throws an AccessViolationException This is what I have... The...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.