通过dll写入txt文件

时间:2017-02-16 19:49:52

标签: c++ dll

我想通过dll写入文本文件,它编译得很好,但它不会输出带有以下代码的文件。当我将项目编译为.exe并运行它时,它可以创建文件并写入文件。

从dll写入我需要做什么? 我想在dll中输出一些数据用于调试目的,而不是从dll返回这些中间数据。我从python /

调用dll

credit.cpp

package LineSegment;

use strict;
use warnings;
use Point;

sub new
{
    my $class = shift;

    my @points;
    if (@_ == 4)
    {
        @points = (
                   Point->new($_[0], $_[1]),
                   Point->new($_[2], $_[3]),
                  );
    }
    else
    {
        @points = @_;
    }
    my $self = \@points;
    bless ($self, $class);
    return $self;
}
sub getA{
    #Issue on get A
    my $self = shift;
    return $self->[0];
}
sub getB{
    #Issue on get B
    my $self = shift;
    return $self->[1];
}
sub setA{
    #Can print correct value. Is the return statement where it goes wrong?
    my $self = shift;
    my $point = $_[0];
    if (@_ > 1)
    {
        $point = Point->new(@_);
    }
    $self->[0] = $point;
}

sub setB{
    my $self = shift;
    my $point = $_[0];
    if (@_ > 1)
    {
        $point = Point->new(@_);
    }
    $self->[1] = $point;
}
1;

credit.def

#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;

double*  _stdcall credit(double* in_array, double a, double b)
{

    ofstream myfile;
    myfile.open("example.txt");
    myfile << "Hi!\n";
    myfile.close();

    return in_array;
}

credit.h

LIBRARY "pass"
;DESCRIPTION 'call dll from python'
EXPORTS
    credit

1 个答案:

答案 0 :(得分:0)

从DLL中尝试使用绝对路径“ C:\\ Temp \\ example.txt”,请确保首先创建了可解决问题的文件夹。

我使用以下波纹管从dll中编写简单的计时日志。

void Log(const std::string& text) {
    std::ofstream log("C:\\logs\\performance.log", std::ofstream::app | std::ofstream::out);
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
        std::chrono::system_clock::now().time_since_epoch()).count();
    auto p = std::chrono::system_clock::now();
    auto t = std::chrono::system_clock::to_time_t(p);
    log << toString(text) << ", Since Epoch: " << toString(ms) << " time: " << toString(std::ctime(&t)) << std::endl;
}
相关问题