Wrong result accessing device by JNA - java

I trying to access a device using JNA
The first command return a statusCode 263 insteadof zero.
I searched and found this code, means "INVALID_DEVICE_NAME". The device's manual say:
The MCI_OPEN_TYPE flag must be specified and the lpstrDeviceType member of the structure identified by lpOpen must contain the device
name (in our case “DictCtrl”).
Is there any error in my code?
The "same" code works in a compiled delphi application, my device is recognized.
public class Main {
public static final int MCI_OPEN = 0x0803;
public static final int MCI_OPEN_TYPE = 0x00002000;
public static final int MCI_STATE_STOP = 0x00000001;
public static final int MCI_OPEN_SHAREABLE = 0x00000100;
public static Winmm winmmLib = Winmm.INSTANCE;
public interface Winmm extends StdCallLibrary {
Winmm INSTANCE = (Winmm) Native.loadLibrary("winmm", Winmm.class);
int mciSendCommandA(int IDDevice, int uMsg, int fdwCommand, Structure dwParam);
}
public static class TagmciOpenParmsa extends Structure {
public int dwCallback;
public int wDeviceID;
public String lpstrDeviceType;
public String lpstrElementName;
public String lpstrAlias;
#Override
protected List getFieldOrder() {
return Arrays.asList(new String[] { "dwCallback", "wDeviceID", "lpstrDeviceType", "lpstrElementName", "lpstrAlias" });
}
}
public static void main(String[] args) {
TagmciOpenParmsa tagmciOpenParmsa = new TagmciOpenParmsa();
tagmciOpenParmsa.lpstrDeviceType = "DictCtrl";
int ret = winmmLib.mciSendCommandA(0, MCI_OPEN, MCI_OPEN_TYPE, tagmciOpenParmsa); // | MixerConstants.MCI_OPEN_SHAREABLE
System.out.println(ret);
}
}

Related

How to update static variable by passing the parameters of the same variable and nonstatic ones into static function in java?

package staticassignment3;
public class Booking {
private String customerEmail;
private int seatsRequired;
private static int seatsAvailable;
private boolean isBooked;
static {
seatsAvailable = 400;
}
public Booking(String customerEmail, int seatsRequired) {
this.customerEmail = customerEmail;
this.seatsRequired = seatsRequired;
}
public String getCustomerEmail() {
return this.customerEmail;
}
public void setCustomerEmail(String customerEmail) {
this.customerEmail= customerEmail;
}
public int getSeatsRequired() {
return this.seatsRequired;
}
public void setSeatsRequired(int seatsRequired) {
this.seatsRequired = seatsRequired;
}
public static int getSeatsAvailable() {
return Booking.seatsAvailable;
}
public static void setSeatsAvailable(int seatsAvailable) {
Booking.seatsAvailable = Booking.seatsAvailable - this.seatsRequired;
}
public boolean isBooked() {
if(Booking.seatsAvailable>= this.seatsRequired) {
Booking.setSeatsAvailable(seatsAvailable);
this.isBooked = true;
}
else {
this.isBooked = false;
}
return isBooked;
}
}
In the above Booking class, I want to update the static variable seatsAvailable by using the static method setSeatsAvailable but I am passing a nonstatic variable in it i.e this.seatsRequired which is not permitted. Is there any alternative to update the seatsAvailable without changing the code so much?
package staticassignment3;
public class Tester {
public static void main(String[] args) {
Booking booking1 = new Booking("jack#email.com", 100);
Booking booking2 = new Booking("jill#email.com", 350);
Booking[] bookings = { booking1, booking2 };
for (Booking booking : bookings) {
if (booking.isBooked()) {
System.out.println(booking.getSeatsRequired()+" seats successfully booked for "+booking.getCustomerEmail());
} else {
System.out.println("Sorry "+booking.getCustomerEmail()+", required number of seats are not available!");
System.out.println("Seats available: "+Booking.getSeatsAvailable());
}
}
}
}
in above Booking class i want to update seatsAvailable static variable by using setSeatsAvailable static method but i am passing nonstatic varible in it i.e this.seatsRequired which is not permitted.is there any alternate to achive the updated seatsAvailable static varibale without doing major changes in code
When calling a static method, there is no assocated object instance. So, it is not valid to use this inside a static method, since this refers to the current object (but you have none).
Specfically, this method isn't correct:
public static void setSeatsAvailable(int seatsAvailable) {
Booking.seatsAvailable = Booking.seatsAvailable - this.seatsRequired;
}
If you want to keep the method static, you could pass an additional parameter to the method – an instance of Booking – and then replace this.seatsRequired with booking.seatsRequired, like this:
public static void setSeatsAvailable(int seatsAvailable, Booking booking) {
Booking.seatsAvailable = Booking.seatsAvailable - booking.seatsRequired;
}

Could not find class error in Autolab assignment

I am trying to sort an ArrayList in increasing order in reference to a certain variable. This is the problem question.
q5: Create a public class named Snow with private instance variables vast, prior, ethnic, and remarkable each of type int. You may add any other methods and variables you'd like to this class.
Outside of Snow (in the Problem Set class) write a public static method named sortSnow that takes an ArrayList of Snows as a parameter and returns void. This method will sort the input by the variable remarkable in increasing order
This is what I wrote.
public class snow implements Comparable<snow> {
private int vast;
private int prior;
private int ethnic;
private int remarkable;
public snow( int vast , int prior, int ethnic ,int remarkable) {
this.vast=vast;
this.prior = prior;
this.ethnic = ethnic;
this.remarkable = remarkable;
}
public int getEthnic() {
return ethnic;
}
public void setEthnic(int ethnic) {
this.ethnic = ethnic;
}
public int getPrior() {
return prior;
}
public void setPrior(int prior) {
this.prior = prior;
}
public int getVast() {
return vast;
}
public void setVast(int vast) {
this.vast = vast;
}
public int getRemarkable() {
return remarkable;
}
public void setRemarkable(int remarkable) {
this.remarkable = remarkable;
}
public int compareTo(snow compareSnow) {
// TODO Auto-generated method stub
int compareThese = ((snow) compareSnow).getRemarkable();
//ascending order
return this.remarkable - compareThese;
}
}
public static void sortSnow(ArrayList<snow>input){
Collections.sort(input);
}
I am not understanding what the error means. The autolab is giving me this error:
Could not find class submission.ProblemSet$Snow
Java is case sensitive i.e. snow is not Snow is not sNoW. Rename your class to Snow and try again. Also, it is ArrayList and not arraylist.
Then to sort a List, you can use Collections.sort.
I think this is you want to achieve
Save below code in file called "Snow.java" compile it and try to run it.
import java.util.ArrayList;
import java.util.Collections;
//As ".java" file can contain only single public java class
//I made Problem set class non-public so we can use its main method
//to run and see output
class ProblemSet {
public static void main(String[] args) {
Snow one = new Snow(1,1,1,1);
Snow two = new Snow(1,1,1,2);
Snow three = new Snow(1,1,1,3);
Snow four = new Snow(1,1,1,4);
Snow five = new Snow(1,1,1,5);
Snow six = new Snow(1,1,1,6);
ArrayList arrayList = new ArrayList();
arrayList.add(one);
arrayList.add(three);
arrayList.add(five);
arrayList.add(two);
arrayList.add(six);
arrayList.add(four);
System.out.println("Without sort");
System.out.println(arrayList);
sortSnow(arrayList);
System.out.println("With sort");
System.out.println(arrayList);
}
//this is your static method which takes argument as array list of Snow
//And it applies sorting logic based on compareTo method which you wrote
//in Snow class. As per java best practice Class name should start with
//Upper case letters and follow camel casing I renamed your class from
//"snow" to "Snow"
public static void sortSnow(ArrayList<Snow> input){
Collections.sort(input);
}
}
//This is you public class Snow
//If you want to keep it in separate java file put it
public class Snow implements Comparable<Snow> {
private int vast;
private int prior;
private int ethnic;
private int remarkable;
public Snow(int vast, int prior, int ethnic, int remarkable) {
this.vast = vast;
this.prior = prior;
this.ethnic = ethnic;
this.remarkable = remarkable;
}
public int getEthnic() {
return ethnic;
}
public void setEthnic(int ethnic) {
this.ethnic = ethnic;
}
public int getPrior() {
return prior;
}
public void setPrior(int prior) {
this.prior = prior;
}
public int getVast() {
return vast;
}
public void setVast(int vast) {
this.vast = vast;
}
public int getRemarkable() {
return remarkable;
}
public void setRemarkable(int remarkable) {
this.remarkable = remarkable;
}
public int compareTo(Snow compareSnow) {
// TODO Auto-generated method stub
int compareThese = ((Snow) compareSnow).getRemarkable();
//ascending order
return this.remarkable - compareThese;
}
//This is added because when you use array list to print
//it will print remarkable of particular Snow object
#Override
public String toString() {
return String.valueOf(remarkable);
}
}

Java variables overwriting

I have a program with a for that take strings from an Arraylist, split them and sends them to a Worker class.
Worker Class:
public class WorkerRSR extends SwingWorker<String, Void>{
private static String urlFrames;
private static String urlImg;
public static int bool;
public static int dist;
public static int numI;
public static int spra;
public static boolean isCoda;
public static int numCoda;
public static String algo;
public WorkerRSR(String urlImg, int dist, int numI, int spra, String algo, String urlFrames, boolean isCoda, int numCoda) {
this.urlImg=urlImg;
this.dist=dist;
this.numI=numI;
this.spra=spra;
this.algo=algo;
this.urlFrames=urlFrames;
this.isCoda = isCoda;
this.numCoda = numCoda;
//FIRST CHECK POINT
}
#Override
protected String doInBackground() throws Exception {
PanelRSR_LRSR.getProgessbar().setIndeterminate(true);
go();
return "";
}
#Override
protected void done() {
System.out.println("Algoritmo RSR esguito");
if(isCoda){
CreateOption.codaCont++;
System.out.println("RSR codaCont: "+CreateOption.codaCont);
if(CreateOption.codaCont==CreateOption.csize){
JOptionPane.showMessageDialog(null,"Coda Eseguita", "Attenzione",JOptionPane.WARNING_MESSAGE);
PanelRSR_LRSR.getProgessbar().setIndeterminate(false);
}
}
else{
PanelRSR_LRSR.getProgessbar().setIndeterminate(false);
JOptionPane.showMessageDialog(null,"Finito RSR", "Attenzione",JOptionPane.WARNING_MESSAGE);
}
}
public static void go() throws IOException{
System.out.println("ESEGUO RSR, attendi...");
//SECOND CHECK POINT
System.out.println("RSR n = "+numI+" codaCont: "+CreateOption.codaCont+" numCoda = "+numCoda);
while(true){
if(numCoda==CreateOption.codaCont)
break;
}
MakeRSR m=new MakeRSR();
String name = urlImg.substring(urlImg.lastIndexOf("\\"),urlImg.lastIndexOf("."));
String output=name.substring(1); //?
String urlOutput=urlFrames+"\\finalRSR\\"+name+"-"+algo+"-dist"+dist+"-n"+numI+"-N"+spra+".png";
m.RSR(urlImg,urlOutput,dist,numI,spra);
}
}
The problem is that this class is going to be called multiple times and every time it overwrite the previous values of the varables: if I check them at the first Checkpoint they are different (probably beacuse the second acquisition yet has to be made), but at the second Checkpoint they are the same.
How can I let them stay different?
These variables shouldn't be static if they are set by the constructor. They should be instance variables, so each instance of your class can have different values.
public class WorkerRSR extends SwingWorker<String, Void>{
private String urlFrames;
private String urlImg;
private int bool;
private int dist;
private int numI;
private int spra;
private boolean isCoda;
private int numCoda;
private String algo;
public WorkerRSR(String urlImg, int dist, int numI, int spra, String algo, String urlFrames, boolean isCoda, int numCoda) {
this.urlImg=urlImg;
this.dist=dist;
this.numI=numI;
this.spra=spra;
this.algo=algo;
this.urlFrames=urlFrames;
this.isCoda = isCoda;
this.numCoda = numCoda;
//FIRST CHECK POINT
}
...
}
You should also change all those variables to be private. If they should be accessed from outside the class, they should be accessed via getter methods.

Can not access members outside

public class TableModel extends AbstractTableModel {
public int page;
public TableModel(Integer p) {
this.page=p;
System.out.println("mm"+page);
}
public void pudata() {
System.out.println(page);
}
//System.out.println("model "+page);
private String[] columnNames = {"groupName","membersCount","previliage"};
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
#Override
public int getRowCount() {
return data.size() ;
}
Can not access variable page in getgroup() method it passes 0 to getgroup() method.
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
Your question is unclear, but I suspect the problem is just that all the instance initializers are being run before the constructor body, so you're seeing the default value for page. You should have something like:
public class TableModel extends AbstractTableModel {
private static final String[] columnNames =
{"groupName","membersCount","previliage"}; // TODO: Fix spelling!
private final int page;
private final List<GroupData> data;
public TableModel(int page) {
this.page = page;
this.data = new DatabaseLayer().getGroup(page);
}
...
}
It's generally a good idea to keep all your instance/static variable declarations in one place (I prefer to keep them at the top, but YMMV) and make them all private to make it easier to reason about how they're used. The main change, however, is moving the new DatabaseLayer ().getGroup(page) code into the constructor.
public class TableModel extends AbstractTableModel {
public int page;
public ArrayList<GroupData> data;
public TableModel(Integer p) {
this.page=p;
this.data = (new DatabaseLayer ()).getGroup(page);
System.out.println("mm"+page);
}
public void pudata() {
System.out.println(page);
}
//System.out.println("model "+page);
private String[] columnNames = {"groupName","membersCount","previliage"};
#Override
public int getRowCount() {
return data.size() ;
}
Refresh your data field every time when you assign a new value to the page field.
public TableModel(int p) {
setPage(p);
}
public void setPage(int p) {
this.page = p;
this.data = new DatabaseLayer ().getGroup(page);
}
This is absolute correct because:
public int page;
default value for page is 0 because its int.
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
Is a variable initialization so before initialization of page you are passing it into .getGroup(page) so default value will pass in that case.
So you have to call getGroup(int) method after page being initialized, one way can be following:
private final List<GroupData> data;
public TableModel(Integer p) {
this.page = p;
this.data = new DatabaseLayer().getGroup(page);
System.out.println("mm"+page);
}

How can I get data of different types from an anonymous class

I have an object that delegates some work to another object which is implementing an interface. Then, I am creating anonymous classes implementing this interface and I would like to get information from these.
Is it okay to use a final array with a size of one as a pointer to a primitve to share data with the anonymous class?
Here is a working example of what I mean :
public class ExampleClass
{
public static final int INVALID_VALUE = -1;
public static void main(final String[] args)
{
final int[] buffer = { INVALID_VALUE }; // buffer is created
final InterfaceA iaObject = new InterfaceA()
{
#Override
public void doStuff(final String paramA)
{
buffer[0] = paramA.length(); // buffer is filled in anonymous class
}
};
final ClassA objA = new ClassA(iaObject);
objA.doStuff("hello, world");
if (buffer[0] == INVALID_VALUE) // buffer is used
{
System.err.println("Invalid length !");
}
else
{
System.err.println("The length is : " + Integer.toString(buffer[0]));
}
}
public static class ClassA
{
private final InterfaceA iaObject;
public ClassA(final InterfaceA iaObject)
{
this.iaObject = iaObject;
}
public void doStuff(final String paramA)
{
this.iaObject.doStuff(paramA);
}
}
public static interface InterfaceA
{
void doStuff(String paramA);
}
}
Thanks
Suggestion: why not using a generic for an out parameter?
interface InterfaceA {
public <T> void doStuff( String paramA, Holder<T> holder );
}
class Holder<T> {
public T t;
}
Full example:
public class ExampleClass
{
public static final int INVALID_VALUE = -1;
public static void main(final String[] args)
{
final InterfaceA< Integer > iaObject = new InterfaceA< Integer >() {
#Override
public Integer doStuff( String paramA, Holder<Integer> holder ) {
return holder.value = paramA.length();
}
};
final ClassA<Integer> objA = new ClassA<>( iaObject );
int result = objA.doStuff("hello, world", new Holder<>( INVALID_VALUE ));
if( result == INVALID_VALUE ) {
System.err.println("Invalid length !");
}
else {
System.err.println("The length is : " + Integer.toString( result ));
}
}
public static class ClassA<T> {
private final InterfaceA<T> iaObject;
public ClassA( final InterfaceA<T> iaObject_ ) {
this.iaObject = iaObject_;
}
public T doStuff( final String paramA, Holder<T> holder ) {
return this.iaObject.doStuff( paramA, holder );
}
}
public static interface InterfaceA<T> {
public T doStuff( String paramA, Holder<T> resultHolder );
}
public static class Holder<T> {
public T value;
public Holder( T value_ ) {
value = value_;
}
}
}
If I understand the gist of your question, you're wondering if it is good design principle to use a final array as a wrapper to share memory between an anonymous inner class and its enclosing class.
In my experience, this is a pretty poor way of sharing data between two objects. It is probably wiser to declare your interface differently. Either return an object or use a generic to specify what type you expect back from your anonymous class.
I think one of the largest problems with your approach is the lack of encapsulation - your InterfaceA implementation uses some "global" data holder (the array), and there is no way to prevent that this array can be used elsewhere, which in turn could lead to all kinds of problems (race conditions or whatever).
A cleaner way would be the definition of some separate class (or interface) with a getInt()-method or something similar.

Categories