473,413 Members | 2,058 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,413 software developers and data experts.

I'm trying to rewrite "c" code to "c#" i'm stuck in some point.

Hi...
I'm new to c# and got some problems in rewriting "C" code in "C#".
I got this error message "You can only take the address of an unfixed expression inside of a fixed statement initializer" and i'm not sure whats wrong??? can anybody light my path?
here's the "C" code:
Expand|Select|Wrap|Line Numbers
  1. USHORT APIENTRY16 PrtOpenDevice (HFILE * _Seg16 PrtHandle, USHORT PrtDriver);
  2. HANDLE PrtHandle;       /* Printer handle */
  3. USHORT PrtDriver;       /* Logical device driver */
  4. USHORT rc;              /* Return code */
  5.  
  6. PrtDriver = 1;          /* 1st logical device driver */
  7.  
  8. rc = PrtOpenDevice (&PrtHandle, PrtDriver);
  9.  
and this is what i have done so far:
Expand|Select|Wrap|Line Numbers
  1. unsafe public partial class Form1 : Form
  2. {
  3.     [System.Runtime.InteropServices.DllImportAttribute("WNTDFPRT.DLL", EntryPoint = "PrtOpenDevice")]
  4.     public static extern ushort PrtOpenDevice([System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.U8)] long *PrtHandle, ushort pPort);
  5.  
  6.     public long PrtHandle = 0;
  7.  
  8.     public Form1()
  9.     {
  10.         InitializeComponent();
  11.         ushort rc = PrtOpenDevice(&PrtHandle, 1);
  12.         MessageBox.Show("return code : " + rc.ToString());
  13.     }
  14. }
  15.  
Jun 17 '10 #1

✓ answered by Christian Binder

http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
This might help you.

It seems as if you've to do:
Expand|Select|Wrap|Line Numbers
  1. fixed(long* pPrtHandle = &PrtHandle) {
  2.   ushort rc = PrtOpenDevice(pPrtHandle, 1);
  3. }
  4.  
instead of ushort rc = PrtOpenDevice(&PrtHandle, 1);

4 1948
Christian Binder
218 Expert 100+
http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
This might help you.

It seems as if you've to do:
Expand|Select|Wrap|Line Numbers
  1. fixed(long* pPrtHandle = &PrtHandle) {
  2.   ushort rc = PrtOpenDevice(pPrtHandle, 1);
  3. }
  4.  
instead of ushort rc = PrtOpenDevice(&PrtHandle, 1);
Jun 17 '10 #2
I try that one, but it returns "PInvokeStackImbalance was detected" instead the fixed-unfixed error.
It seems the "pPrtHandle" contains more than one value.
so the library thinks that "PrtOpenDevice(pPrtHandle, 1)" sending more than two variable.
Jun 17 '10 #3
@ChBinder
I try something that i haven't yet try:
i change the code in BOLD:
Expand|Select|Wrap|Line Numbers
  1. public static extern ushort PrtOpenDevice([System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.U8)] long *PrtHandle, ushort pPort);
  2.  


with:
Expand|Select|Wrap|Line Numbers
  1. public static extern ushort PrtOpenDevice(long *PrtHandle, long pPort);
  2.  


thanks to "ChBinder" for the light on "fixed-unfixed error" advice.
[solved]
Jun 17 '10 #4
Plater
7,872 Expert 4TB
I would have just said maybe this:
Expand|Select|Wrap|Line Numbers
  1. public static extern ushort PrtOpenDevice(ref long PrtHandle, long pPort); 
  2.  
Which I think would do the same thing?
Jun 17 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
3
by: gerald.maher | last post by:
Hi, I am trying to excuate the follwong code: I get the following error: Here is my Lib path:
3
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that...
2
by: tlk | last post by:
Hi, everyone. I'm having a problem that I hope some more experienced developers can help me figure-out. We have a project called Records that has been around for a year or so. We're currently...
3
by: Martin | last post by:
Hi, I have an aspx page with two dropdownlist controls. I update the options in the second ddl based on selection made in the first. I do this with the ICallbackEventHandler interface, as per...
14
by: Paddy3118 | last post by:
This month there was/is a 1000+ long thread called: "merits of Lisp vs Python" In comp.lang.lisp. If you followed even parts of the thread, AND previously used only one of the languages AND...
5
by: Nathan Sokalski | last post by:
I have an ASP.NET application which is giving the following JavaScript error: 'theForm' is undefined However, when I do a View Source one of the <scriptelements is as follows: <script...
5
by: tejesh | last post by:
I am trying to compile the following code int backend_sm_run(struct interface_data *ctx) { xsup_assert((ctx != NULL), "ctx != NULL", TRUE); xsup_assert((ctx->statemachine != NULL),...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.