Porting a simple c program from linux to windows

Utility to monitor network throughput on devices supporting SNMP such as the Vigor 2600, Billion 7402 & Cisco 2600.
Post Reply
Ogden563
New User
Posts: 1
Joined: Tue Jul 09, 2019 7:51 am

Porting a simple c program from linux to windows

Post by Ogden563 »

I want to write a simple c program to do the following. Open a connection to a parallel port, make pin 2 high, make pin 2 low and close the connection. I am using JNI for this, so my Java source file is as follows.

package meas;

Code: Select all

public class Meas {

    public static native boolean open();

    public static native boolean on();

    public static native boolean off();

    public static native boolean close();

}
Note that a Java file should control the parallel port, i.e. decide when it should be high or low. Then, I extracted a c header file using javah.

Code: Select all

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class meas_Meas */

#ifndef _Included_meas_Meas
#define _Included_meas_Meas
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     meas_Meas
 * Method:    open
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_open
  (JNIEnv *, jclass);

/*
 * Class:     meas_Meas
 * Method:    on
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_on
  (JNIEnv *, jclass);

/*
 * Class:     meas_Meas
 * Method:    off
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_off
  (JNIEnv *, jclass);

/*
 * Class:     meas_Meas
 * Method:    close
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_close
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif
Then I implemented this for Linux:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/types.h>
#include <fcntl.h>
#include <meas_Meas.h>

#define BASEPORT 0x378 /* lp1 */

int tem;

/*
 * Class:     meas_Meas
 * Method:    open
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_open(JNIEnv *env, jclass clz) {
    //set permissions to access port
    if (ioperm(BASEPORT, 3, 1)) {
        perror("ioperm");
        exit(1);
    }

    tem = fcntl(0, F_GETFL, 0);
    fcntl(0, F_SETFL, (tem | O_ASYNC));
}

/*
 * Class:     meas_Meas
 * Method:    on
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_on(JNIEnv *env, jclass clz) {
    outb(255, BASEPORT);
}

/*
 * Class:     meas_Meas
 * Method:    off
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_off(JNIEnv *env, jclass clz) {
    outb(0, BASEPORT);
}

/*
 * Class:     meas_Meas
 * Method:    close
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_close(JNIEnv *env, jclass clz) {

    fcntl(0, F_SETFL, tem);
    outb(0, BASEPORT);

    //take away permissions to access port
    if (ioperm(BASEPORT, 3, 0)) {
        perror("ioperm");
        exit(1);
    }
}
I am not a C export, so the code above may look weird. But that does not really matter. What matters is that I want to implement this for Windows also. The goal is to get a dll, just like I already have a libMeas for Linux. I already have MinGW working and all but the problem is that on Windows you can't use sys/io.h. Searching on google for documentation on how to do this results in tutorials on how to write data on the parallel port. I do not want this, I just want to make pin 2 high or low. My guess is that this should be fairly simple.
User avatar
phil
Site Admin
Posts: 7611
Joined: Sun Apr 06, 2003 11:12 pm

Re: Porting a simple c program from linux to windows

Post by phil »

Urm, what is this post doing here/ What has it got to do with network throughput monitoring?
Maybe you meant to post to the InpOut32 forum - that would make MUCH more sense.

Please post to the correct forum - and read the rules (it really does get annoying having to keep repeating this so excuse the frustration!).
---------------
InpOut32 is a simple port driver that WILL allow you to set the pins high or low. You can write directly to the port.
To do this in Windows, you need a signed kernel driver - Inpout32/x64 IS just that - but its old and unsupported (the original author has dissapeared from the net (hopefully not the planet but who knows). It hasn't been updated for may years - it still works with Windows 7,8 and maybe 10 - but I haven't tried it on 10 for a long time. Microsoft *are* changing the rules, requiring stronger driver signatures at which point InpOut32 will stop working unless someone has several hundreds of dollars AND a registered company to purchase a driver signing certificate and resign the driver. The source code is still availabe on my website so its possible - but I'm not going near it as I don't have the time or ability to sign the driver any more.

Still it should work for now, especially on OLDer windows versions.

Regards,
Phil
--[ Phil ]--
--[ Administrator & XMBC Author ]--
Logitech G9/G604/M720/MX518, Microsoft Intellimouse, Trust 16341 BT Mouse
Windows 10 x64, AMD Ryzen 5900x, MSI x570 Tomahawk, 32GB DDR4,
nVidia RTX 2070s, Evo 970 1Tb NVME, 2x2TB WD Black (RAID1)
Post Reply